在UIButton中为所有状态定位图像

可能重复:
UIButton:如何使用imageEdgeInsets和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]];

有一些UIButton属性是专门为此制作的

@property(nonatomic) UIEdgeInsets contentEdgeInsets
@property(nonatomic) UIEdgeInsets imageEdgeInsets
@property(nonatomic) UIEdgeInsets titleEdgeInsets

使用此属性来调整和重新定位按钮图像的有效绘制矩形。 您可以为四个插图(顶部,左侧,底部,右侧)中的每一个指定不同的值。 正值缩小或缩小,使边缘靠近按钮的中心。 负值会扩大或突破该边缘。 使用UIEdgeInsetsMake函数为此属性构造一个值。 默认值是UIEdgeInsetsZero。


@jspooner完成修改imageview框架和标签框架后,设置按钮框架可能会起到一定作用。


您应该创建您的图像,然后将其应用于按钮状态:

UIImage* reviewButtonImage = [UIImage initWithContentsOfFile:/*path*/];
[reviewButton setImage:reviewButtonImage forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateDisabled|UIControlStateSelected];
链接地址: http://www.djcxy.com/p/28245.html

上一篇: Position image in UIButton for all states

下一篇: UIButton: how to center an image and a text using imageEdgeInsets and titleEdgeInsets?