【问题标题】:how to set size UIView in auto layout programmatically swift?如何以编程方式快速设置自动布局中的 UIView 大小?
【发布时间】:2015-05-24 07:44:09
【问题描述】:

我使用此代码制作了两个UIViewAuto Layout

class ViewController: UIViewController {

    var view_constraint_V:NSArray = [NSLayoutConstraint]();
    var originalValue:CGFloat = 0.0;
    var offsetValue:CGFloat = -100;

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        var topView = UIView()
        topView.setTranslatesAutoresizingMaskIntoConstraints(false)
        topView.backgroundColor = UIColor.blackColor()

        var bottomView = UIView()
        bottomView.setTranslatesAutoresizingMaskIntoConstraints(false)
        bottomView.backgroundColor = UIColor.lightGrayColor()

        let button   = UIButton()
        button.backgroundColor = UIColor.darkGrayColor()
        button.setTitle("Click me!", forState: UIControlState.Normal)
        button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTranslatesAutoresizingMaskIntoConstraints(false)

        self.view.addSubview(topView);
        self.view.addSubview(bottomView);
        bottomView.addSubview(button)

        // constraints
        let viewsDictionary = ["top":topView,"bottom":bottomView, "button":button]

        //position constraints
        let view_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[top]-10-|",options: NSLayoutFormatOptions(0),metrics: nil, views: viewsDictionary)
        let view_constraint_H2:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[bottom]-10-|",options: NSLayoutFormatOptions(0),metrics: nil, views: viewsDictionary)

        view_constraint_V = NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[top(bottom)]-[bottom]-10-|", options: NSLayoutFormatOptions.AlignAllLeading, metrics: nil, views: viewsDictionary)

        view.addConstraints(view_constraint_H)
        view.addConstraints(view_constraint_H2)
        view.addConstraints(view_constraint_V)

        originalValue = (view_constraint_V[1] as NSLayoutConstraint).constant;
      //println((view_constraint_V[1] as NSLayoutConstraint))
        (view_constraint_V[1] as NSLayoutConstraint).constant = offsetValue

        // Position button
        let control_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-30-[button]-30-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
        let control_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-30-[button(50)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)

        bottomView.addConstraints(control_constraint_H)
        bottomView.addConstraints(control_constraint_V)

    }
    /**
        Example of dynamically changing the constraints when clicking on a button.
    */
    func buttonAction(sender:UIButton!)
    {
        if (view_constraint_V[1].constant == offsetValue) {
            (view_constraint_V[1] as NSLayoutConstraint).constant = originalValue
        } else {
            (view_constraint_V[1] as NSLayoutConstraint).constant = offsetValue
        }
    }
}

现在,我想以编程方式将顶视图大小设置为底视图大小的 1/3,但我不知道该怎么做。

【问题讨论】:

  • 减少自动布局样板代码的建议是使用PureLayout
  • @Schemetrical 我的朋友 PureLayout 适合 swift 吗?
  • 所有 Objective-C 库都可以与 swift 一起使用。
  • 使用界面构建器而不是代码,通过代码创建 UI 是最不舒服的事情。

标签: ios swift uiview autolayout


【解决方案1】:

这将起作用:

let heightConstraint = NSLayoutConstraint(item: topView, 
                                          attribute: NSLayoutAttribute.Height,
                                          relatedBy: NSLayoutRelation.Equal,
                                          toItem: bottomView,
                                          attribute: NSLayoutAttribute.Height,
                                          multiplier: 0.33, constant: 0)
self.view.addConstraint(heightConstraint)

let widthConstraint = NSLayoutConstraint(item: topView, 
                                          attribute: NSLayoutAttribute.Width,
                                          relatedBy: NSLayoutRelation.Equal,
                                          toItem: bottomView,
                                          attribute: NSLayoutAttribute.Width,
                                          multiplier: 0.33, constant: 0)

self.view.addConstraint(widthConstraint)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 2013-04-02
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多