您的位置:首页 > 其它

自定义一个图片在上,文字在下的按钮

2016-04-11 21:26 453 查看
//
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: