ios - Swift: Custom UIView not resizing to constraints -


i have created custom uiview loaded xib file. adding view stackview, , setting width constraint on item.

it's working if storyboard, if i'm doing swift, can't view stretch constraint. stackview allocating space view, view doesn't stretch space.

custom view swift code:

import foundation import uikit  @ibdesignable class tabbutton: uiview {  @iboutlet weak var label: uilabel!  @ibinspectable var tabtext: string? {     {         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 properties here      // 2. call super.init(frame:)     super.init(frame: frame)      // 3. setup view .xib file     xibsetup() }  required init(coder adecoder: nscoder) {     // 1. setup properties here      // 2. call super.init(coder:)     super.init(coder: adecoder)!      // 3. setup view .xib file     xibsetup() }  // our custom view xib file var view: uiview!  func xibsetup() {     view = loadviewfromnib()      // use bounds not frame or it'll offset     view.frame = bounds      // make view stretch containing view     view.autoresizingmask = [.flexiblewidth, .flexibleheight]      // adding custom subview on top of our view (over 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 } } 

and viewcontroller adding 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) } 

i want tabbutton "ignore" it's frame , resize according height of stackview , width constraint i'm setting.

what missing?

update:

my constraints on custom view (basically view label - plan use more complex layouts well):

my constraints

try this:

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) 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -