可能重复:
UIButton: how to center an image and a text using imageEdgeInsets and titleEdgeInsets?
我正在自定义我的UIButton的图像和标签的位置,但它只设置在初始状态。什么是黑客,我如何确保所有4个按钮状态的定位?
这是我正在使用的代码。中毒适用于第一个州,但会在其他州重置。
CGRect imgRect = reviewsButton.imageView.frame;
imgRect.origin.x = 10;
imgRect.origin.y += 4;
reviewsButton.imageView.frame = imgRect;
CGRect lblrect = reviewsButton.titleLabel.frame;
lblrect.origin.x = 85;
reviewsButton.titleLabel.frame = lblrect;
[reviewsButton setBackgroundColor:[UIColor colorWithRed:0.192 green:0.198 blue:0.206 alpha:0.15]];
答案 0 :(得分:5)
有专门为此
制作的UIButton
个属性
@property(nonatomic) UIEdgeInsets contentEdgeInsets
@property(nonatomic) UIEdgeInsets imageEdgeInsets
@property(nonatomic) UIEdgeInsets titleEdgeInsets
使用此属性可以调整按钮图像的有效绘图矩形并重新定位。您可以为四个插图(顶部,左侧,底部,右侧)中的每一个指定不同的值。正值会缩小或缩小,使其靠近按钮的中心。负值会扩展或偏离该边缘。使用UIEdgeInsetsMake函数为此属性构造值。默认值为UIEdgeInsetsZero。
答案 1 :(得分:0)
@jspooner完成更改imageview框架和标签框后设置按钮框架可能会有效。
答案 2 :(得分:0)
您应该创建图像,然后将其应用于按钮状态:
UIImage* reviewButtonImage = [UIImage initWithContentsOfFile:/*path*/];
[reviewButton setImage:reviewButtonImage forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateDisabled|UIControlStateSelected];
答案 3 :(得分:0)
我无法使用所需的布局来使用UIButton,所以我创建了一个自定义的UIView并处理那里的触摸。