Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton titleEdgeInsets [duplicate]

Tags:

ios

uibutton

titleEdgeInsets used to align the title and the picture on the button. I did image on the left and title on the right.But when I click on the button, the title is shifting to the left. Thank you

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;

status is normal

status is clicked

like image 384
hiwordls Avatar asked Aug 28 '13 09:08

hiwordls


1 Answers

This is the example another way for setting the title and image in straight ----> :) try this way,

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];


like image 59
Shankar BS Avatar answered Nov 05 '22 17:11

Shankar BS