swift - Swift:以编程方式自动布局图像
问题描述
在我的 Swift 应用程序中,我插入了这样的图像:
let ImageNavig = "5.jpg"
let image1 = UIImage(named: ImageNavig)
let image1View = UIImageView(image: image1!)
let yCenter1 = self.view.center.y
let Image1Size = CGPoint(x: 420, y: 240)
image1View.frame = CGRect(x: 0, y: -10, width: Image1Size.x, height: Image1Size.y)
view.addSubview(image1View)
但是,当我使用较旧的 Iphone 运行项目时,结果是这样的:
如何在没有情节提要的情况下以编程方式插入自动布局?
解决方案
如果您在旧 iphone 中遇到图像宽度问题,那是因为您已经为图像提供了固定宽度,使用下面的代码它将根据 iphone 宽度动态获取宽度
let Image1Size = CGPoint(x: self.view.frame.width, y: 240)
代替
let Image1Size = CGPoint(x: 420, y: 240)
推荐阅读
- python - URL 处的 NoReverseMatch
- javascript - 无法在 addname 处读取 null 的属性“值”
- java - 如何将带有提醒的事件添加到日历?
- unity3d - unity输入陀螺仪获得无偏角
- node.js - 添加新类别时 Mongoose 模式转换错误
- c - 如何制作动态分配的结构数组?
- bash - bash 脚本:在寻找匹配的 ``' 时出现意外的 EOF
- xhtml - 使用多个 DTD(来自 mozilla.org)
- java - 如何将 RecordInterceptor 设置为 ConcurrentKafkaListenerContainerFactory
- tableau-api - 是否可以在计算度量中访问预测结果?