UIButton error with image and caption

It does not look like UIButton instrinsicSize and / or sizeToFit is considering the left-margin insert, or something is confused with my expectations.

To demonstrate, I have two custom type buttons in the view, as with the "Button" header. I want to add an image to the button to the left of the title.

var image = UIImage(named: "circledPlay") image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) self.button1.setImage(image, forState: UIControlState.Normal) self.button1.invalidateIntrinsicContentSize() self.button1.sizeToFit() self.button2.setImage(image, forState: UIControlState.Normal) self.button2.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0) self.button2.invalidateIntrinsicContentSize() self.button2.sizeToFit() 

The result is as follows:

screen shot

Notice that the second button is truncated.

So my question would be if someone had seen this before (and hopefully has a solution), or am I confused, and it behaves as expected (and hopefully knows the right way to do this)?

+6
source share
1 answer

As the documentation says, for titleEdgeInsets : "The button does not use this property to determine intrinsicContentSize and sizeThatFits: ". Thus, setting titleEdgeInsets simply moves the title label, but does not affect the size of the button. If you want the button to have more content additions, set contentEdgeInsets as well. I don't think you need to call either sizeToFit or invalidateIntrinsicContentSize (but not sure about that).

+9
source

Source: https://habr.com/ru/post/985479/


All Articles