圆形图像扭曲与自动布局

时间:2015-09-02 01:53:50

标签: ios xcode swift

当我工作时,我的故事板设置为4.7“。当我在iP6模拟器上运行我的应用程序时,我的圆形UIImageView看起来很漂亮和圆形。就像这样:

enter image description here

然而,当我在所有其他模拟器中运行应用程序时,我的循环UIImageView遇到了失真

这是iP5:

enter image description here

这是iP6 +:

enter image description here

这是我用来绕过UIImageView的代码:

profileImageView.layer.cornerRadius = profileImageView.frame.size.width / 2
profileImageView.clipsToBounds = true

UIImageView的大小正确调整,正如您所见,圆度变得时髦。以下是我设置的约束以防万一:

1:1比例,对齐中心x以查看,对齐顶部视图= 15,对齐底部视图= -100

图像以完美的正方形开始,所以我认为使用1:1的比例设置它会始终保持圆形,我使用的代码......我在这里做错了什么?非常感谢!

3 个答案:

答案 0 :(得分:2)

之前我遇到过这个问题,你的代码是100%正确的,问题出在自动布局和这些限制上:

  

将顶部对齐到视图= 15,将底部对齐到视图= -100

由于屏幕尺寸发生变化,您无法与顶视图和底视图对齐。您可以做的只是与顶部对齐并找到另一个约束来维持大小。配置文件图片对我有用的是尺寸限制。

之前,有上限和下限: enter image description here

之后,具有向上和固定的宽度约束: enter image description here

答案 1 :(得分:0)

你把代码放在哪里了?

视图大小更改时,viewDidLayoutSubviews不会更改。您需要在VC中的layoutSubviews或者视图类中printf的末尾更新它(以匹配视图的大小)。

答案 2 :(得分:0)

尝试这绝对会起作用。 swift 代码

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    perform(#selector(self.setCircleForImage(_:)), with: pickedImage, afterDelay: 0)
}


 @objc func setCircleForImage(_ imageView : UIImageView){
    imageView.layer.cornerRadius = pickedImage.frame.size.width/2
    imageView.clipsToBounds = true
}
  

UIImage必须保持1:1的宽高比

相关问题