UIImageView 框架未正确放置在父 ImageView 上
UIImageView frame not placing correctly on the parent ImageView
我有一个调色板图像视图,因为我想根据从后端获取的 x 轴和 y 轴放置一个加号图标 (imageView)。如果我得到 x = 0 和 y = 0 加号的框架放置正确
对于 y 轴,如果我将调色板 imageView 的高度设置为加号图标框架的 y 轴,图标不会到达实际的 (0,0)
。
我使用的代码如下
let cWidth = self.colorPalleteImageView.frame.size.width // 348
let cHeight = self.colorPalleteImageView.frame.size.height // 378.5
let imageView = UIImageView(image: appImages.roundPlusIcon.image)
imageView.frame = CGRect(x: 0, y: cHeight, width: 22, height: 22)
colorPalleteImageView.addSubview(imageView)
我正在用 iPad 12.9 英寸模拟器检查这个。我是否缺少任何实现 x=0 和 y=0 的东西,如果我将 colorPaletteImage 的宽度赋予 plusIconImageView 的 x 轴,它不会在 x 轴结束,它停留在宽度结束之前imageview,我不知道为什么会这样,需要帮助
imageView.frame = CGRect(x: cWidth, y: cHeight, 宽度: 22, 高度: 22)
必须是:
imageView.frame = CGRect(x: 0, y: 0, 宽度: 22, 高度: 22)
因为轴是:
0,0----------------> x
| ▢
|
|
|
V
使用 0,0 可以将小图像的左上角放在大图像的左上角。
您可以通过将 height
属性 修改为超级视图的边界高度来解决此问题。您需要知道 frame
和 bounds
之间的区别才能理解为什么会发生这种情况。简单来说,frame 是关于它的超级视图的 CGRect,而 bounds 是关于它自己坐标的 CGRect
。
网上有很多详细的解释,只需要google frame vs bounds,玩多了就会习惯了。
这是解决此问题的方法。
let cHeight = self.colorPalleteImageView.bounds.size.height
对不起..我回来了
我做到了(为了遵循你的代码,我强制值..)
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var colorPalleteImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let v = self.view
print(v!.frame)
let cHeight = CGFloat(378.5)
colorPalleteImageView.frame.size.width = 348
colorPalleteImageView.frame.size.height = cHeight
let plusImg = UIImage(named: "plus")
let imageView = UIImageView(image: plusImg)
imageView.frame = CGRect(x: 0, y: cHeight-22, width: 22, height: 22)
colorPalleteImageView.addSubview(imageView)
}
}
我得到了:(在 iPad)
您使用的框架尺寸之前框架已完成由自动布局设置。
我建议使用约束,但如果您想坚持使用框架坐标...
- 在
viewDidLoad()
中添加 "round plus" 图标 imageView
- 在
viewDidLayoutSubviews()
或viewDidAppear()
中设置其frame.origin
我有一个调色板图像视图,因为我想根据从后端获取的 x 轴和 y 轴放置一个加号图标 (imageView)。如果我得到 x = 0 和 y = 0 加号的框架放置正确
对于 y 轴,如果我将调色板 imageView 的高度设置为加号图标框架的 y 轴,图标不会到达实际的 (0,0)
我使用的代码如下
let cWidth = self.colorPalleteImageView.frame.size.width // 348
let cHeight = self.colorPalleteImageView.frame.size.height // 378.5
let imageView = UIImageView(image: appImages.roundPlusIcon.image)
imageView.frame = CGRect(x: 0, y: cHeight, width: 22, height: 22)
colorPalleteImageView.addSubview(imageView)
我正在用 iPad 12.9 英寸模拟器检查这个。我是否缺少任何实现 x=0 和 y=0 的东西,如果我将 colorPaletteImage 的宽度赋予 plusIconImageView 的 x 轴,它不会在 x 轴结束,它停留在宽度结束之前imageview,我不知道为什么会这样,需要帮助
imageView.frame = CGRect(x: cWidth, y: cHeight, 宽度: 22, 高度: 22)
必须是:
imageView.frame = CGRect(x: 0, y: 0, 宽度: 22, 高度: 22)
因为轴是:
0,0----------------> x
| ▢
|
|
|
V
使用 0,0 可以将小图像的左上角放在大图像的左上角。
您可以通过将 height
属性 修改为超级视图的边界高度来解决此问题。您需要知道 frame
和 bounds
之间的区别才能理解为什么会发生这种情况。简单来说,frame 是关于它的超级视图的 CGRect,而 bounds 是关于它自己坐标的 CGRect
。
网上有很多详细的解释,只需要google frame vs bounds,玩多了就会习惯了。
这是解决此问题的方法。
let cHeight = self.colorPalleteImageView.bounds.size.height
对不起..我回来了
我做到了(为了遵循你的代码,我强制值..)
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var colorPalleteImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let v = self.view
print(v!.frame)
let cHeight = CGFloat(378.5)
colorPalleteImageView.frame.size.width = 348
colorPalleteImageView.frame.size.height = cHeight
let plusImg = UIImage(named: "plus")
let imageView = UIImageView(image: plusImg)
imageView.frame = CGRect(x: 0, y: cHeight-22, width: 22, height: 22)
colorPalleteImageView.addSubview(imageView)
}
}
我得到了:(在 iPad)
您使用的框架尺寸之前框架已完成由自动布局设置。
我建议使用约束,但如果您想坚持使用框架坐标...
- 在
viewDidLoad()
中添加 "round plus" 图标 imageView
- 在
viewDidLayoutSubviews()
或viewDidAppear()
中设置其
frame.origin