[英]Set the center of a UIButton programmatically - SWIFT
I have a UIButton
inside a UIView
, my UIButton
has constraints which I set in storyboard.我在
UIView
有一个UIButton
,我的UIButton
有我在故事板中设置的约束。 Now, I want to set the center of the UIButton
at the center of the UIView
.现在,我想将
UIButton
的中心设置在UIView
的中心。 How will I do that programmatically?我将如何以编程方式做到这一点?
Try .center
property like尝试
.center
属性,如
myButton.center = self.view.center
You can also specify x
and y
If you need.如果需要,您还可以指定
x
和y
。
myButton.center.x = self.view.center.x // for horizontal
myButton.center.y = self.view.center.y // for vertical
This approach is using Using NSLayoutConstraint where self.cenBut is the IBoutlet for your button.这种方法使用 Using NSLayoutConstraint 其中 self.cenBut 是您按钮的 IBoutlet。
func setupConstraints() {
let centerX = NSLayoutConstraint(item: self.cenBut, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
let centerY = NSLayoutConstraint(item: self.cenBut, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
let height = NSLayoutConstraint(item: self.cenBut, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 22)
self.cenBut.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraints([centerX, centerY, height])
}
In viewDidLoad()在 viewDidLoad()
self.view.removeConstraints(self.view.constraints)
self.setupConstraints()
since you are using constraints, you won't be able to set the center so you need to use constraints to set the center as well:由于您使用了约束,因此您将无法设置中心,因此您还需要使用约束来设置中心:
let centerYCon = NSLayoutConstraint(item: labelMessage,
attribute: .CenterY,
relatedBy: .Equal,
toItem: contentView,
attribute: .CenterY,
multiplier: 1.0,
constant: 0.0);
contentView.addConstraint(centerYCon);
let centerXCon = NSLayoutConstraint(item: labelMessage,
attribute: .CenterX,
relatedBy: .Equal,
toItem: contentView,
attribute: .CenterX,
multiplier: 1.0,
constant: 0.0);
contentView.addConstraint(centerXCon);
I'm assuming that you know what "programmatically" means with autolayout constraints.我假设您知道“以编程方式”对自动布局约束意味着什么。 If you are truly using autolayout then you must have set the properties of your button to setTranslatesAutoresizingMaskIntoConstraints to false.
如果您真的在使用自动布局,那么您必须将按钮的属性设置为 setTranslatesAutoresizingMaskIntoConstraints 为 false。
This can be done in 2 ways:这可以通过两种方式完成:
myButton.center = view.center //view is your parentView
myButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
myButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
I should be我应该
yourButton.setCenter(self.view.center);
But keep in mind that if you programmatically set the frame of a view, your layout constraints might not work as intended.但请记住,如果您以编程方式设置视图的框架,您的布局约束可能无法按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.