2015-10-15 108 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