I create view
init(){
super.init(frame: .zero)
}
After creating view I want to set my subviews
func setViews(){
self.addSubview(labelView)
self.addConstraintsWithFormat("V:|-10-[v0]-10-|", views: labelView)
self.addConstraintsWithFormat("H:|-10-[v0]-10-|", views: labelView)
}
Everything works, but I have warning that
Probably at least one of the constraints in the following list is one you
don't want. Try this: (1) look at each constraint and try to figure out
which you don't expect; (2) find the code that added the unwanted
constraint or constraints and fix it. (Note: If you're seeing
NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the
documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7f87ea8e6520 V:|-(10)-[BIPartners.labelView:0x7f87eb56be20] (Names: '|':BIPartners.labelView:0x7f87eb56a320 )>",
"<NSLayoutConstraint:0x7f87ea8e6af0 V:[BIPartners.labelView:0x7f87eb56be20]-(10)-| (Names: '|':BIPartners.labelView:0x7f87eb56a320 )>",
"<NSLayoutConstraint:0x7f87e868bdc0 '_UITemporaryLayoutHeight' V:[BIPartners.labelView:0x7f87eb56a320(0)]>"
)
I know that problem is when I set frame to zero. I set frame to zero and I set vertical margin to 10. How should I solve this problem?
You should solve this problem by lowering the priority of the label's bottom pinning constraint to something less than 1000:
self.addConstraintsWithFormat("V:|-10-[v0]-10(@750)-|", views: labelView)
This will disambiguate your constraints, preserving your bottom padding and the intrinsic height of the label.
Basically, this ambiguity occurs when you want to use intrinsic content size AND have the label pinned on both sides.
Of course, there are other ways to obtain the desired spacing (margins) but this answer is nearest to what you are trying to do.