调整UIImageView的大小和限制

问题描述 投票:-2回答:2

我想让我的应用程序以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
ios swift constraints
2个回答
0
投票

你可以试试

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()

0
投票

如果您使用Storyboard创建了视图,则可以为IBOutlet的高度或宽度创建UIImageview。例如:

@IBOutlet weak var imageHeightConstraint: NSLayoutConstraint!

然后你可以以编程方式更改高度:

imageHeightConstraint.constant = 50

并强制视图立即更新其布局:

self.view.layoutIfNeeded()
© www.soinside.com 2019 - 2024. All rights reserved.