UIButton at the same time set title and image, the default is the picture in the left text on the right, such as 1, in many cases we want the picture in the next, such as 2, just a few lines of code to be simple, you can achieve.
(1) Because more than one button needs to be processed, the implementation code is encapsulated as a method, passing each UIButton instance as a parameter, with the following code:
- -(void) Initbutton: (uibutton*) btn{
- Btn.contenthorizontalalignment = uicontrolcontenthorizontalalignmentcenter;//to center the picture and text horizontally
- [Btn Settitleedgeinsets:uiedgeinsetsmake (Btn.imageView.frame.size.height,-btn.imageview.frame.size.width, 0.0,0.0)];//text distance from the top border increases the height of the ImageView, the distance from the left box reduces the width of the ImageView, the distance from the bottom and right borders is unchanged
- [Btn setimageedgeinsets:uiedgeinsetsmake (0.0, 0.0,0.0,-btn.titlelabel.bounds.size.width)];//picture distance to the right border to reduce the width of the picture, Other non-side
- }
(2) then pass the button
[OBJC]View Plain Copy
- [Self initButton:self.btn1];
- [Self initButton:self.btn2];
- [Self initButton:self.btn3];
- [Self initButton:self.btn4];
- [Self initButton:self.btn5];
- [Self initButton:self.btn6];
- [Self initButton:self.btn7];
(3) Run the viewing effect.
iOS button settings picture on the text below