如何将ImageView放置在UIView的中心?

let dailyUIView = UIView(frame: CGRect(x: 0, y: 420, width: view.frame.size.width, height: 40))
dailyUIView.layer.masksToBounds = true
dailyUIView.backgroundColor = UIColor(named: "dailyTest")
scrollView.addSubview(dailyUIView)

let weatherIconImageView = UIImageView(frame: CGRect(x: 70, y: 0, width: 60, height: 60))
weatherIconImageView.image = UIImage(named: "01d")
dailyUIView.addSubview(weatherIconImageView)

推荐答案

   let dailyUIView = UIView(frame: CGRect(x: 0, y: 420, width: view.frame.size.width, height: 40))
   dailyUIView.layer.masksToBounds = true
   dailyUIView.backgroundColor = UIColor(named: "dailyTest")
   scrollView.addSubview(dailyUIView)

   let weatherIconImageView = UIImageView(frame: CGRect(x: 70, y: 0, width: 60, height: 60))
   weatherIconImageView.image = UIImage(named: "01d")
   dailyUIView.addSubview(weatherIconImageView)
        
   weatherIconImageView.translatesAutoresizingMaskIntoConstraints = false         
   NSLayoutConstraint.activate([
            weatherIconImageView.widthAnchor.constraint(equalToConstant: 60),
            weatherIconImageView.heightAnchor.constraint(equalToConstant: 60),
            weatherIconImageView.centerXAnchor.constraint(equalTo: dailyUIView.centerXAnchor),
            weatherIconImageView.centerYAnchor.constraint(equalTo: dailyUIView.centerYAnchor),
        ])

Ios相关问答推荐

ios PWA从外部链接返回后降低了innerheight

在TVOS中访问SWIFT中的objc++函数时发送给类的无法识别的 Select 符

执行devicectl:ProcessException时出错:进程异常退出:错误:命令超时超过5.0秒

如何在后台显示 Swift UI 中的通讯通知?

如何在 SwiftUI 中通过通用 @ObservedObject 进行切换?

包装在 AnyView 中时 UIViewControllerRepresentable 无法正常工作

setNeedsLayout 和 setNeedsDisplay

光标未显示在 UITextView 中

在 iOS 编程中使用 Storyboard 代替 xib 文件有什么好处?

UIScrollview 获取touch 事件

关闭所有打开的视图控制器的单个函数

Swift枚举继承

如何在 iPhone 中为透明的 PNG 图像着色?

iOS 10 / Xcode 8 设备上的 NSLog 似乎被截断了?为什么?

iOS:以编程方式制作屏幕截图的最快、最高效的方法是什么?

以编程方式创建按钮并设置背景图像

iOS:设备旋转后如何运行函数(Swift)

iOS SDK - 以编程方式生成 PDF 文件

Swift 中的日期到毫秒和回溯到日期

闭包不能隐式捕获变异的 self 参数