The Titleedgeinsets and imageedgeinsets in UIButton can manage the layout of the image and title in the button.
If the understanding of it is not deep enough, with pure digital layout management, after continuous debugging, or can try out, but if the picture size or text length, but also to do it again.
As a procedural ape, we should not give up any chance of laziness.
- By default, the picture is left, the text is in the right, and the vertical center is displayed, such as:
button.titleEdgeInsets = UIEdgeInsetsZero;button.imageEdgeInsets = UIEdgeInsetsZero;
- When you set the following layout, the pictures and text are centered.
button.Titleedgeinsets=Uiedgeinsetsmake(0,-button.ImageView.Framesize. Width00 Button.imageedgeinsets = uiedgeinsetsmake (0, 0, 0,-button.titlelabel.frame.size.width); //because of iOS8 in the size of 0, with the above set up a problem, modify it can titlelabel button. Imageedgeinsets = uiedgeinsetsmake (0 00-button. Titlelabel. Intrinsiccontentsize. Width
- If you want the picture to be on, the text is below, the horizontal center is displayed, then the following settings are required:
button.Titleedgeinsets=Uiedgeinsetsmake(0,-button.ImageView.Frame.Size.Width,-button.ImageView.Frame.Size.Height,0);Button.imageedgeinsets = Uiedgeinsetsmake (-button.titlelabel.frame.size.height, 0, 0,- Button.titleLabel.frame.size.width);//Because the size of the Titlelabel in the iOS8 is 0, there is a problem with the above setting, modify the button. Imageedgeinsets = uiedgeinsetsmake(-button. Titlelabel. Intrinsiccontentsize. Height, 0, 0, -button. Titlelabel. Intrinsiccontentsize. width);
If you think the picture and text are too close, a little bit apart:
CGFloatOffset=40.0f;button.Titleedgeinsets=Uiedgeinsetsmake(0,-button.ImageView.Frame.Size.Width,-button.ImageView.Frame.Size.Height-Offset/2,0);Button.imageedgeinsets = Uiedgeinsetsmake (-BUTTON.TITLELABEL.FRAME.SIZE.HEIGHT-OFFSET/2, 0, 0,- Button.titleLabel.frame.size.width);//Because the size of the Titlelabel in the iOS8 is 0, there is a problem with the above setting, modify the button. Imageedgeinsets = uiedgeinsetsmake(-button. Titlelabel. Intrinsiccontentsize. Height-offset/2, 0, 0, -button. Titlelabel. Intrinsiccontentsize. width);
- Text left-aligned, picture right-aligned
button.Titleedgeinsets=Uiedgeinsetsmake(0,-button.ImageView.Frame.Size.Width-button.Frame.Size.Width+button.Titlelabel.Intrinsiccontentsize.Width,0,0);button.Imageedgeinsets=uiedgeinsetsmake(0, 0, 0, -button. Titlelabel. Frame. Size. Width - button. Frame. Size. Width + button. ImageView. Frame. Size. width);
How to lay out a uibutton containing the image and title