1 首先继承UIButton
2 实现imageRectForContentRect titleRectForContentRect
// 内部图片的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat imageW = contentRect.size.width;
CGFloat imageH = contentRect.size.height * IWTabBarButtonImageRatio;
return CGRectMake(0,imageW,imageH);
}
// 内部文字的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat titleY = contentRect.size.height * IWTabBarButtonImageRatio;
CGFloat titleW = contentRect.size.width;
CGFloat titleH = contentRect.size.height - titleY;
return CGRectMake(0,titleY,titleW,titleH);
}
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 图标居中 self.imageView.contentMode = UIViewContentModeCenter; // 文字居中 self.titleLabel.textAlignment = NSTextAlignmentCenter; } return self; }
原文链接:https://www.f2er.com/swift/323793.html