//
// MenuItemButton.h
#import <UIKit/UIKit.h>
@interface MenuItemButton : UIButton
@end
//
// MenuItemButton.m
#import "MenuItemButton.h"
#define kImageRatio 0.8
@implementation MenuItemButton
- (void)awakeFromNib
{
[self setup];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
}
- (void)setup
{
self.imageView.contentMode = UIViewContentModeCenter;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
}
// 如果通过代码设置子控件位置,都是在layoutSubviews里面
- (void)layoutSubviews
{
[super layoutSubviews];
// imageView
CGFloat imageX = 0;
CGFloat imageY = 0;
CGFloat imageW = self.bounds.size.width;
CGFloat imageH = self.bounds.size.height * kImageRatio;
self.imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
// titleLabel
CGFloat labelY = imageH;
CGFloat labelH = self.bounds.size.height - labelY;
self.titleLabel.frame = CGRectMake(imageX, labelY, imageW, labelH);
}
@end