自定义一个图片在上,文字在下的按钮
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
相关文章推荐
- 通过hexo+NexT构建静态博客
- 行为型模式之观察者模式
- nginx1.8.1反向代理、负载均衡功能的实现
- STM32f103 FLASH 操作
- 【BZOJ4518】征途,斜率优化DP
- 高低算法
- ACM-2000
- 侧滑
- nginx1.8.1反向代理、负载均衡功能的实现
- HDU 4109 Instrction Arrangement
- delete析构的调用
- Fragment,顺序跳转的实现
- 正整数分割
- Android 广播------实现强制下线功能
- ListView 中的 监听器
- 第一个hibernate5.1.0--向数据库中插入一条数据
- 广东工业大学2016校赛 Problem A: Krito的讨伐
- webservice入门笔记二soap消息的处理
- AndroidManifest.xml 配置文件
- 数据结构 - 1基本算法设计与分析