Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在操场上布置约束_Ios_Swift_Iphone_Autolayout_Swift Playground - Fatal编程技术网

Ios 在操场上布置约束

Ios 在操场上布置约束,ios,swift,iphone,autolayout,swift-playground,Ios,Swift,Iphone,Autolayout,Swift Playground,我试图重现这样一个场景,在不同的屏幕尺寸下,红色和蓝色矩形可以占据相同的宽度和高度(以及它们之间的相同间距) 我正在使用NSLayoutConstraint(我知道现在首选锚点,只是尝试探索基础知识)。我在swift游乐场尝试了以下代码: import Foundation import UIKit import PlaygroundSupport class MyViewController : UIViewController { var firstColorView: UIV

我试图重现这样一个场景,在不同的屏幕尺寸下,红色和蓝色矩形可以占据相同的宽度和高度(以及它们之间的相同间距)

我正在使用NSLayoutConstraint(我知道现在首选锚点,只是尝试探索基础知识)。我在swift游乐场尝试了以下代码:

import Foundation
import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    var firstColorView:  UIView!
    var secondColorView: UIView!

    override func viewWillAppear(_ animated: Bool) {
        var myView: UIView!
        myView = view
        myView.backgroundColor = .white

        firstColorView = UIView()
        secondColorView = UIView()
        firstColorView.backgroundColor = .red
        secondColorView.backgroundColor = .blue

        myView.addSubview(firstColorView)
        myView.addSubview(secondColorView)


        //myView.translatesAutoresizingMaskIntoConstraints = false
        //view.addSubview(myView)

        //  horizontal constraints
        let left_constraint = NSLayoutConstraint(item: firstColorView, attribute: .leftMargin, relatedBy: .equal, toItem: myView, attribute: .left, multiplier: 1.0, constant: 20)
        let middle_constraint = NSLayoutConstraint(item: secondColorView, attribute: .leftMargin, relatedBy: .equal, toItem: firstColorView, attribute: .right, multiplier: 1.0, constant: 10)
        let right_constraint = NSLayoutConstraint(item: myView, attribute: .rightMargin, relatedBy: .equal, toItem: secondColorView, attribute: .right, multiplier: 1.0, constant: 20)
        let width_constraint = NSLayoutConstraint(item: firstColorView, attribute: .width, relatedBy: .equal, toItem: secondColorView, attribute: .width, multiplier: 1.0, constant: 0)

        // vertical constraints
        let top_constraint1 = NSLayoutConstraint(item: firstColorView, attribute: .top, relatedBy: .equal, toItem: myView, attribute: .top, multiplier: 1.0, constant: 10)
        let top_constraint2 = NSLayoutConstraint(item: secondColorView, attribute: .top, relatedBy: .equal, toItem: myView, attribute: .top, multiplier: 1.0, constant: 10)
        let bottom_constraint1 = NSLayoutConstraint(item: myView, attribute: .bottom, relatedBy: .equal, toItem: firstColorView, attribute: .bottom, multiplier: 1.0, constant: 10)
        let bottom_constraint2 = NSLayoutConstraint(item: myView, attribute: .bottom, relatedBy: .equal, toItem: secondColorView, attribute: .bottom, multiplier: 1.0, constant: 0)

        NSLayoutConstraint.activate([left_constraint, middle_constraint, right_constraint, width_constraint, top_constraint1, top_constraint2, bottom_constraint1, bottom_constraint2])
        self.view.layoutIfNeeded()
    }
}


// Present the view controller in the Live View window
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = MyViewController()

但它显示的只是一个白色屏幕,其宽度与iphone不匹配。我做错了什么?为什么我看不到红色和蓝色的屏幕?

你错过了吗

firstColorView.translatesAutoresizingMaskIntoConstraints = false  
secondColorView.translatesAutoresizingMaskIntoConstraints = false
你错过了

firstColorView.translatesAutoresizingMaskIntoConstraints = false  
secondColorView.translatesAutoresizingMaskIntoConstraints = false
谢谢从中可以看出,此属性对于故事板和基于nib的视图为true,而对于以编程方式创建的视图为false。这似乎很奇怪,不是吗。自动布局现在是推荐的解决方案,不确定为什么默认情况下会使用自动调整大小。谢谢!从中可以看出,此属性对于故事板和基于nib的视图为true,而对于以编程方式创建的视图为false。这似乎很奇怪,不是吗。自动布局现在是推荐的解决方案,不确定为什么默认情况下会使用自动调整大小。