I am an Android developer, having tried my hand on Xcode, and so far it has been unpleasant. What I'm trying to do is have a custom view that has three subtasks:
- UIImageView (for the icon)
- UILabel (for name)
- UILabel (for content)
I want the height of the content label to increase and contract to wrap the text that it contains (e.g. Android wrap_content ). And then, I want the user view to also grow and contract to wrap all three subviews.
However, I cannot, for my life, find out how these automatic layouts / constraints work.
01) How do I increase / decrease the size of a UILabel so that it matches the text contained in it?
02) How do I make my custom view height increase / decrease to fit the subviews it contains?
override func layoutSubviews() { super.layoutSubviews() img_icon = UIImageView() txt_title = UILabel() txt_content = UILabel() img_icon.backgroundColor = Palette.white img_icon.image = icon txt_title.text = title txt_title.textAlignment = .Center txt_title.font = UIFont(name: "Roboto-Bold", size:14) txt_title.textColor = Palette.txt_heading1 txt_content.text = content txt_content.textAlignment = .Center txt_content.font = UIFont(name: "Roboto-Regular", size:12) txt_content.textColor = Palette.txt_dark txt_content.numberOfLines = 0 txt_content.preferredMaxLayoutWidth = self.frame.width txt_content.lineBreakMode = NSLineBreakMode.ByWordWrapping self.backgroundColor = Palette.white addSubview(img_icon) addSubview(txt_title) addSubview(txt_content) let txt_content_x = NSLayoutConstraint(item: txt_content, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0) let txt_content_y = NSLayoutConstraint(item: txt_content, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 80) let txt_content_w = NSLayoutConstraint(item: txt_content, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: 0) let txt_content_h = NSLayoutConstraint(item: txt_content, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40) txt_content.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activateConstraints([ txt_content_x, txt_content_y, txt_content_w, txt_content_h ]) }
I understand that in the above code I tried, my height is set to constant 40 . This happens only because I do not know how to achieve what I want.
[EDIT]
I tried to set the height limit to greater or equal, but it just resets Xcode.
[EDIT]
This causes Xcode to crash if I try to view it, but works fine in the simulator. The question is why?
Now my height limit is:
let txt_content_h = NSLayoutConstraint(item: txt_content, attribute: .Height, relatedBy: .GreaterThanOrEqual, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40)
It works in a simulator and has the desired behavior. However, if I open the storyboard containing the view, it will work. It is definitely that line of code since changing it back to .Equal resolves the failure.
[EDIT]
My interim fix:
#if TARGET_INTERFACE_BUILDER //use .Equal for height constraint #else //use .GreaterThanOrEqual for height constraint #endif
This way it does not break Xcode and still does the way I want it on the simulator.
[EDIT]
I deleted the check in front of the processor because I realized that there is no such thing as this, and it still works. I swear I havenβt changed anything.
I'm close to abandoning iOS development because the interface developer continues to break Xcode for no reason when everything works in the simulator. Then I make some meaningless changes, and it works great.