首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS -以编程方式使用自动布局将UIView放置在superview的中心

iOS -以编程方式使用自动布局将UIView放置在superview的中心
EN

Stack Overflow用户
提问于 2012-12-23 21:36:59
回答 2查看 28.3K关注 0票数 26

如何使用自动布局以编程方式将UIView设置为其superview的中心?

代码语言:javascript
运行
复制
UIButton* viewObj = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[viewObj setTranslatesAutoresizingMaskIntoConstraints:NO];
[viewObj setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:viewObj];

NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem:viewObj
                                                      attribute:NSLayoutAttributeCenterX
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeCenterX 
                                                     multiplier:1.0
                                                       constant:0];
[self.view addConstraint:cn];
cn = [NSLayoutConstraint constraintWithItem:viewObj 
                                  attribute:NSLayoutAttributeCenterY
                                  relatedBy:NSLayoutRelationEqual 
                                     toItem:self.view
                                  attribute:NSLayoutAttributeCenterY
                                 multiplier:1.0
                                   constant:0];
[self.view addConstraint:cn];

上面的代码适用于UIButton,但我无法将第一行替换为适用于UIView的代码。

我试过了

代码语言:javascript
运行
复制
UIView* viewObj = [UIView alloc] initWithFrame:CGRectZero];

但是视图不会在模拟器中显示。

有什么建议吗?

谢谢!

EN

Stack Overflow用户

发布于 2015-12-28 02:01:37

UIButtonUIImageViewUIlabelUITextField可以根据其内容属性自动设置其大小。UIImageView的宽度和高度由它可能包含的UIImage设置。UILabel的大小将取决于它的文本。UIButton的宽度和高度由标题和它的图像定义(您可以通过Intrinsic Content Size了解更多信息)。

因此,当您想要将UIButtonUILabelUITextFieldUIImageView放在具有自动布局的UIView中时,几乎在所有情况下都不必为它们的宽度和高度创建约束。您只需要为它们设置水平和垂直约束。

但是,使用自动布局,没有子视图的UIView不能依赖任何东西来设置其大小,除非您为它提供一些任意的宽度和高度约束。根据你的需要,你可以用3种不同的方法来解决这个问题。

纯自动布局样式

在这里,我们将UIView的宽度和高度直接设置为自动布局约束:

代码语言:javascript
运行
复制
override func viewDidLoad() {
    super.viewDidLoad()

    let newView = UIView()
    newView.backgroundColor = .redColor()
    newView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(newView)

    // Auto layout code using anchors (iOS9+)
    let horizontalConstraint = newView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor)
    let verticalConstraint = newView.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor)
    let widthConstraint = newView.widthAnchor.constraintEqualToAnchor(nil, constant: 100)
    let heightConstraint = newView.heightAnchor.constraintEqualToAnchor(nil, constant: 100)
    NSLayoutConstraint.activateConstraints([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint])
}

自动调整蒙版大小样式

在这里,我们用宽度和高度初始化UIView,使它的中心和superview的中心相等,并创建一些自动调整大小的蒙版。然后,我们要求UIKit将这些自动调整大小的蒙版转换为自动布局约束:

代码语言:javascript
运行
复制
override func viewDidLoad() {
    super.viewDidLoad()
    
    let newView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
    newView.backgroundColor = .redColor()
    
    newView.center = CGPointMake(view.bounds.midX, view.bounds.midY)
    newView.autoresizingMask = [.FlexibleLeftMargin, .FlexibleRightMargin, .FlexibleTopMargin, .FlexibleBottomMargin]

    newView.translatesAutoresizingMaskIntoConstraints = true // default is true
    view.addSubview(newView)
}

子类样式

在这里,我们创建UIView的一个子类并覆盖它的intrinsicContentSize()方法(declaration),以便它返回所需的大小:

代码语言:javascript
运行
复制
import UIKit

class CustomView: UIView {
    override func intrinsicContentSize() -> CGSize {
        return CGSize(width: 100, height: 100)
    }
}

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let newView = CustomView()
        newView.backgroundColor = .redColor()
        newView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(newView)
        
        let horizontalConstraint = NSLayoutConstraint(item: newView,
            attribute: .CenterX,
            relatedBy: .Equal,
            toItem: view,
            attribute: .CenterX,
            multiplier: 1,
            constant: 0)
        view.addConstraint(horizontalConstraint)
        let verticalConstraint = NSLayoutConstraint(item: newView,
            attribute: .CenterY,
            relatedBy: .Equal,
            toItem: view,
            attribute: .CenterY,
            multiplier: 1,
            constant: 0)
        view.addConstraint(verticalConstraint)
    }
    
}
票数 6
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14011395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档