This question already has answers here:
Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets

(14个回答)


7年前关闭。




titleEdgeInsets用于对齐标题和按钮上的图片。我在左边做了图像,在右边做了标题,但是当我点击按钮时,标题就向左移动了。谢谢
UIImage *image = [UIImage imageNamed:imageName];
    [self setImage:image forState:UIControlStateNormal];

CGSize imageSize = self.imageView.frame.size;
CGSize titleSize = self.titleLabel.frame.size;

self.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
self.titleEdgeInsets = UIEdgeInsetsMake(0, ((self.frame.size.width-titleSize.width)/2)-imageSize.width, 0, 0);
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

最佳答案

这是直接在标题中设置标题和图像的另一种方法-> :)
尝试这种方式

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(0, 0, self.view.bounds.size.width, 40);
[aButton setImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];
[aButton setTitle:@"Rate us on app store" forState:UIControlStateNormal];
[aButton setTitleEdgeInsets:UIEdgeInsetsMake(2, 50, 2, 20)];//set ur title insects also
[aButton setImageEdgeInsets:UIEdgeInsetsMake(2, -200, 2, 2)];//make negative edge for left side
[aButton setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:aButton];

关于ios - UIButton titleEdgeInsets,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18484747/

10-12 04:42