UIButton with title under the imageview

image, ios, text, uibutton, uiedgeinsets

Solution

The ultimate and stable solution is to use frame, not `EdgeInset` solution like this:

@interface UIButton (UIButtonExt)
(void)centerImageAndTitleEx;
@end

@implementation UIButton (UIButtonExt)

(void)centerImageAndTitleEx
{
CGRect frame = self.imageView.frame;

frame = CGRectMake(truncf((self.bounds.size.width - frame.size.width) / 2), 10.0f, frame.size.width, frame.size.height);

self.imageView.frame = frame;

frame = self.titleLabel.frame;

frame = CGRectMake(truncf((self.bounds.size.width - frame.size.width) / 2), self.bounds.size.height - frame.size.height - 5.0, frame.size.width, frame.size.height);

self.titleLabel.frame = frame;
}

@end

Problem

I want to creat an UIButton programmatically with the title under the imageView. Size of the button : 170 * 120 Size of the imge : 50 * 50 Size of the title : depend of the text. I know I'have to use but I don't know how : ``` [_button setTitleEdgeInsets:UIEdgeInsetsMake(0.f, 0.f, 0.f, 0.f)]; [_button setImageEdgeInsets:UIEdgeInsetsMake(0.f, 0.f, 0.f, 0.f)]; ``` I think I should calculate the size of the title and then Use the EdgeInsets. Thank you.

Original source