Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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_Uiview_Autolayout_Nslayoutconstraint - Fatal编程技术网

Ios 如何以编程方式添加具有动态宽度的约束

Ios 如何以编程方式添加具有动态宽度的约束,ios,swift,uiview,autolayout,nslayoutconstraint,Ios,Swift,Uiview,Autolayout,Nslayoutconstraint,我是iOS开发新手。我想构建没有故事板或xib/nib的布局。因此,我尝试以编程方式添加约束。 我搜索了一些关于以编程方式添加约束的教程。但是视图无法正确显示 我正在ViewController类中尝试以下代码: override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let test

我是iOS开发新手。我想构建没有故事板或xib/nib的布局。因此,我尝试以编程方式添加约束。 我搜索了一些关于以编程方式添加约束的教程。但是视图无法正确显示

我正在ViewController类中尝试以下代码:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let testView = UIView()
    self.view.addSubview(testView)

    // Add Constraints
    self.view.translatesAutoresizingMaskIntoConstraints = false
    testView.translatesAutoresizingMaskIntoConstraints = false

    let top = NSLayoutConstraint(item: testView, attribute: .top, relatedBy: .equal
        , toItem: self.view, attribute: .top, multiplier: 1, constant: 50.0)
    let bottom = NSLayoutConstraint(item: testView, attribute: .bottom, relatedBy: .equal
        , toItem: self.view, attribute: .bottom, multiplier: 1, constant: -50.0)
    let leading = NSLayoutConstraint(item: testView, attribute: .leading, relatedBy: .greaterThanOrEqual, toItem: self.view, attribute: .leading, multiplier: 1, constant: 50.0)
    let trailing = NSLayoutConstraint(item: testView, attribute: .trailing, relatedBy: .greaterThanOrEqual, toItem: self.view, attribute: .trailing, multiplier: 1, constant: -50.0)
    self.view.addConstraints([top, bottom, leading, trailing])
}
通常,它们不需要在教程中定义视图大小或宽度和高度约束。但是这些视图可以显示在他们的教程中。在我的例子中,即使设置了顶部、底部、前导和尾随约束,我的testView也无法在应用程序中显示它。我错过什么了吗?有什么问题吗

其中一个教程:

编辑: 让我再解释一下。我想创建一个适用于通用设备的
UIView
ui视图
具有带有
常量10的顶部、底部、前导和尾随约束。因此,我不需要设置
UIView
的大小


这是一个高度等于80的屏幕底部视图约束示例:

var yourView = UIView()
yourView.translateAutoresizingMaskIntoConstraints = false
yourSuperView.addSubview(yourView)

// Pin the leading edge of yourView  to the leading edge of the main view
yourView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true

// Pin the trailing edge of yourView to the leading trailing edge
yourView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true

// Pin the bottomedge of yourView to the margin's leading edge
yourView .bottomAnchor.constraintEqualToAnchor(view.bottomAnchor).active = true

// The height of your view
yourView.heightAnchor.constraintEqualToConstant(80).active = true
你有两个问题:

  • 不要将self.view.translatesAutoresizingMaskIntoConstraints=false设置为。您只需为要添加的子视图设置
    translatesAutoResizengMaskintoConstraints

  • 您使用的是
    .greaterThanOrEqual
    约束,而不是
    .equal
    约束。这样做的问题是,会留下很多摇摆的空间,并且您得到的值会导致
    testView
    具有
    0
    宽度。如果更改为
    .equal
    ,将正确约束这些值


  • 欢迎来到SO。在提问之前,你已经检查过所有类似的问题了吗?比如这个例子有大量的答案和解释:我以前检查过这个问题。在最高回答率中,
    UIView
    的大小被定义为宽度:100,高度:100。我已经更新了我的问题。请检查,谢谢。请尝试添加视图。如果其他解决方案都不适用于您或此处的多个答案,请在添加约束后添加所需的布局。避免在多个问题上发布您答案的精确副本。如果您觉得问题重复,您可以发布评论并链接到您的答案,您也可以将其标记。
    testView
    在我删除
    self.view.translatesAutoResizezingMacSkintoConstraints=false
    并更改
    。大于或等于
    。equal
    将更新的代码附加到问题,但不要删除原始代码。你把两者都改成相等了吗?