我想让我的应用程序以UIImageView或UIbutton编程调整大小?
我试过这样的
let imageView = UIImageView(frame: CGRect(x: self.view.center.x, y: self.view.center.y + 400, width: self.view.frame.maxX - 100, height: self.view.frame.midY - 150))
imageView.center = self.view.center
你可以试试
var centerCon:NSLayoutConstraint!
let imageView = UIImageView(frame:.zero)
view.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
centerCon = imageView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
centerCon.isActive = true
NSLayoutConstraint.activate([
imageView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
imageView.widthAnchor.constraint(equalToConstant: self.view.frame.maxX - 100),
imageView.heightAnchor.constraint(equalToConstant: self.view.frame.midY - 150)
])
然后改变
centerCon.constant += 200
self.view.layoutIfNeeded()
如果您使用Storyboard
创建了视图,则可以为IBOutlet
的高度或宽度创建UIImageview
。例如:
@IBOutlet weak var imageHeightConstraint: NSLayoutConstraint!
然后你可以以编程方式更改高度:
imageHeightConstraint.constant = 50
并强制视图立即更新其布局:
self.view.layoutIfNeeded()