【问题标题】:Swift: Custom UIView not resizing to constraintsSwift:自定义 UIView 没有调整到约束
【发布时间】:2015-10-15 08:18:31
【问题描述】:

我创建了一个从 XIB 文件加载的自定义 UIView。然后我将视图添加到堆栈视图,并在项目上设置宽度约束。

如果我从情节提要中执行此操作,它会完美运行,但是如果我从 Swift 中执行此操作,我无法将视图拉伸到约束。 stackview 正在为视图分配空间,但视图没有拉伸到该空间。

自定义查看swift代码:

import Foundation
import UIKit

@IBDesignable class TabButton: UIView {

@IBOutlet weak var label: UILabel!

@IBInspectable var TabText: String? {
    get {
        return label.text
    }
    set(TabText) {
        label.text = TabText
        label.sizeToFit()
    }
}

override func intrinsicContentSize() -> CGSize {
    return CGSize(width: UIViewNoIntrinsicMetric, height: UIViewNoIntrinsicMetric)
}

override init(frame: CGRect) {
    // 1. setup any properties here

    // 2. call super.init(frame:)
    super.init(frame: frame)

    // 3. Setup view from .xib file
    xibSetup()
}

required init(coder aDecoder: NSCoder) {
    // 1. setup any properties here

    // 2. call super.init(coder:)
    super.init(coder: aDecoder)!

    // 3. Setup view from .xib file
    xibSetup()
}

// Our custom view from the XIB file
var view: UIView!

func xibSetup() {
    view = loadViewFromNib()

    // use bounds not frame or it'll be offset
    view.frame = bounds

    // Make the view stretch with containing view
    view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

    // Adding custom subview on top of our view (over any custom drawing > see note below)
    addSubview(view)
}

func loadViewFromNib() -> UIView {

    let bundle = NSBundle(forClass: self.dynamicType)
    let nib = UINib(nibName: "TabButton", bundle: bundle)
    let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

    self.roundCorners([.TopLeft, .TopRight], radius: 10)

    return view
}
}

这是将视图(tabButton)添加到堆栈视图(tabBar)的视图控制器:

@IBOutlet weak var tabBar: UIStackView!

override func viewDidLoad() {
    super.viewDidLoad()

    let tabButton = TabButton(frame: CGRectZero)
    tabButton.label.text = "All Videos"

    tabButton.backgroundColor = UIColor.blackColor()

    let widthConstraint = NSLayoutConstraint(item: tabButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
    tabButton.addConstraint(widthConstraint)

    tabBar.insertArrangedSubview(tabButton, atIndex: 0)
}

我希望 tabButton “忽略”它的框架并根据堆栈视图的高度和我设置的宽度限制调整大小。

我错过了什么?

更新:

我对自定义视图的限制(基本上只是一个带有标签的视图 - 但我也计划将其用于更复杂的布局):

【问题讨论】:

  • 您想以编程方式添加约束,是吗?
  • 是的,因为视图是以编程方式添加的。

标签: ios swift uiview autolayout


【解决方案1】:

试试这个:

let widthConstraint = NSLayoutConstraint (item: your_item_here, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: your_value_here)
self.view.addConstraint(widthConstraint)

【讨论】:

  • 添加时没有任何反应。在我将项目添加到堆栈视图之后,我正在这样做。如果我在出现运行时错误之前这样做。 "'NSInternalInconsistencyException', 原因:'不可能设置没有为约束准备的视图层次结构的布局。'"
  • 您可以在此处粘贴您查看结果的图像吗?
  • postimg.org/image/nty10iqk5 霓虹绿条是堆栈视图中的另一个视图。 stackview 在每个站点中都有相同数量的空间,所以我们可以看到约束正在正常工作,它只是不会调整自定义视图的内容。
【解决方案2】:

至少您应该使用sizeThatFits: 定义自定义视图的大小 .不幸的是,我看不到您的布局约束来判断它们是否合适。

【讨论】:

  • 谢谢,我已经为问题添加了自定义视图的约束截图。如何在 SizeThatFits 中定义自定义视图的大小?我猜这是基于superview设置的约束?
  • 看起来很普通。 sizeThatFits 怎么样?你试过吗?
  • 是的,不会改变任何东西。如果我使用 CGRectMake 而不是使用 CGRectZero 设置 tabButton 的框架大小,我可以正确调整视图的大小。但我想用宽度限制来做。
【解决方案3】:

堆栈视图管理其子视图的布局并自动为您应用布局约束。因此请尝试将 tabButton 添加到 StackView 中,而不受任何约束。

好的,那就看看这个thread

希望这能解决您的问题。

【讨论】:

  • 但我希望自定义视图具有特定的大小,而堆栈视图中的另一个视图可以填充其余部分。这就是为什么我必须设置自定义视图的宽度。
猜你喜欢
  • 2021-06-23
  • 2014-11-21
  • 2022-01-22
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
  • 2017-06-24
  • 2021-05-16
  • 2021-01-14
相关资源
最近更新 更多