ios 以编程方式设置 UIButton 的中心 - SWIFT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32219161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:24:13  来源:igfitidea点击:

Set the center of a UIButton programmatically - SWIFT

iosswiftuiviewuibutton

提问by thedansaps

I have a UIButtoninside a UIView, my UIButtonhas constraints which I set in storyboard. Now, I want to set the center of the UIButtonat the center of the UIView. How will I do that programmatically?

我有一个UIButton内部 a UIView,我UIButton在故事板中设置了约束。现在,我想将 的中心设置UIButtonUIView. 我将如何以编程方式做到这一点?

回答by iRiziya

Try .centerproperty like

尝试.center像这样的属性

myButton.center = self.view.center

You can also specify xand yIf you need.

如果需要,您还可以指定xy

myButton.center.x = self.view.center.x // for horizontal
myButton.center.y = self.view.center.y // for vertical

回答by Prabhu.Somasundaram

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

回答by Loxx

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。

回答by OutOfBounds

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.

但请记住,如果您以编程方式设置视图的框架,您的布局约束可能无法按预期工作。

回答by Ashish Gupta

This can be done in 2 ways:

这可以通过两种方式完成:

  • myButton.center = view.center //view is your parentView

  • myButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = truemyButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

  • myButton.center = view.center //view is your parentView

  • myButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = truemyButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true