2015-10-15 135 views
1

我已经创建了一个自定义UIView从XIB文件加载。然后,我将视图添加到堆栈视图,并为该项目设置宽度约束。Swift:自定义UIView不调整约束

如果我从故事板上做到这一点,它的工作是非常完美的,但是如果我是从Swift开始做的话,我无法将视图拉伸到约束条件。堆栈视图为视图分配空间,但视图不会伸展到空间。

自定义视图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)到stackview(的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为“忽略“它的框架和根据堆栈视图的高度和我设置的宽度约束来调整大小。

我错过了什么?

UPDATE:

我的自定义视图约束(基本上只是带有标签的观点 - 但是我计划更复杂的布局,以及使用此):

My constraints

+0

要以编程方式添加约束,是吗? –

+0

是的,因为视图是以编程方式添加的。 – Whooper

回答

0

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

+0

谢谢,我添加了自定义视图的问题截图。 如何在SizeThatFits中定义自定义视图的大小?我猜这是基于superview设置的约束? – Whooper

+0

看起来很平常。 sizeThatFits怎么样?你尝试过吗? –

+0

是的,不会改变任何东西。 如果我使用CGRectMake而不是使用CGRectZero来设置tabButton的框架大小,我可以正确调整视图的大小。但我想用宽度约束来做。 – Whooper

0

堆栈视图管理其子视图的布局,并自动为您应用布局约束。所以尝试添加tabButton而没有任何约束到StackView。

OK,然后看看这个thread

希望这将解决您的问题。

+0

但我希望自定义视图具有特定的大小,并在堆栈视图中填充其他视图。这就是为什么我必须设置自定义视图的宽度。 – Whooper

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) 
+0

添加时没有任何反应。我在将项目添加到堆栈视图后执行此操作。如果我在得到运行时错误之前执行此操作。 “'NSInternalInconsistencyException',原因是:'无法使用视图层次结构来设置布局,但没有准备好约束'。” – Whooper

+0

您可以在这里粘贴您的视图结果的图像吗? –

+0

http://postimg.org/image/nty10iqk5/ 霓虹绿条是stackview中的另一个视图。 stackview在每个站点中都有相同的空间量,所以我们可以看到约束正在运行,它不会调整自定义视图的内容大小。 – Whooper