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;
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With