Setting the position of setImage and setTitle of UIButton
I want to have a large button the spans the length of my table cells that contains a setImage icon on the right and a setTitle on the left. However, what happens by default is that everything is aligned to the left.
Is there a way to change the position of the UIButton image开发者_开发知识库 and title views? I want everything to be in one button so that if someone clicks the title the icon will change its state as well.
Make your image the same size as the button (i.e. pad it out with transparency) so things go where you want them to be. Say you have an image that 40 x 40, but you want it on the right side of a button 300 x 40. Just increase the size of the image with transparency to have 260 pixels of nothing on the left. For your text, you can use the alignment icons in Interface builder to put the title where you want it.
What I did was subclass UIButton and set [self titleLabel] and [self imageView] in the layoutSubview method.
#import "CustomDetailTableButton.h"
@implementation CustomDetailTableButton
- (void)layoutSubviews
{
[super layoutSubviews];
UILabel *titleLabel = [self titleLabel];
CGRect fr = [titleLabel frame];
fr.origin.x = 10.0;
fr.origin.y = 5.0;
[[self titleLabel] setFrame:fr];
UIImageView *imageView = [self imageView];
CGRect iFrame = [imageView frame];
iFrame.origin.x = 300.0;
iFrame.origin.y = 7.0;
[[self imageView] setFrame:iFrame];
}
@end
精彩评论