UIButton(改变Title和image位置)

时间:2021-10-18 17:51:08

UIButton *btn = [[UIButton alloc] init];

[btn setFrame:frame];

[btn setTitleColor:titleColor forState:UIControlStateNormal];

[btn setTitle:title forState:UIControlStateNormal];

[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

[btn setImage:normalI forState:UIControlStateNormal];

  //按钮内容对齐方式

btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

//按钮image位置

btn.imageEdgeInsets = UIEdgeInsetsMake(0,btn.titleLabel.bounds.size.width, 0,-btn.titleLabel.bounds.size.width);

//按钮title的位置

btn.titleEdgeInsets = UIEdgeInsetsMake(0,-btn.imageView.image.size.width, 0, btn.imageView.image.size.width);

  //按钮图片自适应

btn.imageView.contentMode= UIViewContentModeScaleAspectFit;