Swift无法在xib uiview中更改imageview框架

时间:2017-01-31 08:21:44

标签: ios swift uiview uiimageview swift3

我正在使用Swift 3并尝试更改xib内部的UIImageView宽度和高度

这是我的代码:

let view = Bundle.main.loadNibNamed("Custom", owner: self, options: nil)?[0] as! UIView           
view.frame = CGRect(x: CGFloat(i) * self.sView.frame.size.width, y: self.sView.frame.origin.y, width: self.sView.frame.size.width, height: self.sView.frame.size.height)    

let imageView = view.viewWithTag(1) as! UIImageView

当我尝试像这样更改imageview框架时

imageView.frame = CGRect(x: imageView.frame.origin.x, y: imageView.frame.origin.y - 40, width: 200, height: imageView.frame.height)
当我尝试运行它时没有任何变化,请帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

我最终在我的xib中为我的约束设置了标识符 并像我这样从我的控制器中调用它

for subview in view.subviews {
            for constraint in subview.constraints as [NSLayoutConstraint] {
                if constraint.identifier == "imageWidth" {
                    constraint.constant = view.frame.width * 0.5
                }
            }
        }

我可以控制控制器的宽度,高度,x和y