ios - preferredMaxLayoutWidth not working in Swift? -


i have tried make uilabel width using preferredmaxlayoutwidth no matter won't work. can me? have tries many different combinations make work.

@ibaction func addbottomtextbutton(sender: anyobject) {     if addbottomtextfield.text.isempty == false {       let halfscreenwidth = screensize.width * 0.5       let bottomscreenposition = screensize.width       memebottomtext = addbottomtextfield.text       fontname = "impact"       let memebottomtextcaps = memebottomtext.uppercasestring // --> string!        labelbottom.text = memebottomtextcaps       labelbottom.textcolor = uicolor.blackcolor()       labelbottom.textalignment = .center       labelbottom.font = uifont(name: fontname, size: 32.0)       labelbottom.sizetofit()        labelbottom.userinteractionenabled = true       labelbottom.adjustsfontsizetofitwidth = true       labelbottom.numberoflines = 1       labelbottom.preferredmaxlayoutwidth = screensize.width       labelbottom.settranslatesautoresizingmaskintoconstraints(true)        var r = cgfloat(halfscreenwidth)       var s = cgfloat(bottomscreenposition)       labelbottom.center = cgpoint(x: r, y: s)        self.view.addsubview(labelbottom)       self.view.addconstraint(nslayoutconstraint(item: labelbottom, attribute:          nslayoutattribute.top, relatedby: nslayoutrelation.equal, toitem: labelbottom,               attribute: nslayoutattribute.bottom, multiplier: 1, constant: 0))        dismisskeyboard()    } } 

judging code i'd problem haven't got constraints setup correctly , you're mixing using nslayoutconstraints setting position using center , setting size using sizetofit.

firstly, in constraint you've setup you're relating labelbottom (the item argument) (the toitem argument). i'm not sure trying achieve that? i'd recommend having @ tutorials on autolayout if you're unfamiliar concepts. here's one: http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1

secondly, small point, on line let memebottomtextcaps = memebottomtext.uppercasestring you've written // --> string. easier way remind of variable type when looking @ code use: let memebottomtextcaps: string = memebottomtext.uppercasestring.

thirdly, preferredmaxlayoutwidth isn't used set width of uilabel - that's frame (or nslayoutconstraints if you're using autolayout).

lets on it!

here's example of how create label pinned bottom edge of container view , not allowed wider it's container: (keep in mind can done in ib)

class viewcontroller: uiviewcontroller {     override func viewdidload() {         super.viewdidload()          let label = uilabel()         // 1.         label.settranslatesautoresizingmaskintoconstraints(false)         // 2.         label.text = // put text here.          // 3.         self.view.addsubview(label)          // 4.         let pintobottomconstraint = nslayoutconstraint(item: label,             attribute: .bottom,             relatedby: .equal,             toitem: self.view,             attribute: .bottom,             multiplier: 1.0,             constant: -8.0)          // 5.         let horizontalconstraints = nslayoutconstraint.constraintswithvisualformat("|-8-[label]-8-|",             options: .directionleadingtotrailing,             metrics: nil,             views: ["label" : label])          // 6.         self.view.addconstraint(pintobottomconstraint)         self.view.addconstraints(horizontalconstraints)     } } 

the following referrers commented numbers in code above.

1. need set settranslatesautoresizingmaskintoconstraints false stop constraints being created otherwise conflict constraints we're going create later. here's apple have it:

because autoresizing mask naturally gives rise constraints specify view’s position, view wish apply more flexible constraints must set ignore autoresizing mask using method. should call method programmatically created views. views created using tool allows setting constraints should have set already.

2. need make sure put own text here, otherwise code won't run.

3. label must added view hierarchy before adding constraints between , it's superview! otherwise, in case, you'll runtime error saying:

unable parse constraint format: unable interpret '|' character, because related view doesn't have superview |-8-[label]-8-|

this due our horizontalconstraints needing know label's superview (the superview denoted "|") label doesn't have superview.

4. pintobottomconstraint constraint says. constant of -8 specifies want label 8 points bottom of container view.

we don't need create constraint specify label's size - that's intrinsic property of uilabel determined, example, number of lines , font.

5. horiontalconstraints created using visual format language. here's tutorial: http://code.tutsplus.com/tutorials/introduction-to-the-visual-format-language--cms-22715 basically, "|-8-[label]-8-|" creates constraints pin left , right edges of label left , right edges of superview.

6. add constraints!

this looks like:

enter image description here

i hope answers question.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -