1014-32-首页13-cell的结构分析---导航栏中间title位置的按钮的尺寸设置---setFrame----
2016-03-12 13:10
381 查看
#import "HWTitleButton.h"
#define HWMargin 5
@implementation HWTitleButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont boldSystemFontOfSize:17];
[self setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateSelected];
}
return self;
}
// 目的:想在系统计算和设置完按钮的尺寸后,再修改一下尺寸
/**
* 重写setFrame:方法的目的:拦截设置按钮尺寸的过程
* 如果想在系统设置完控件的尺寸后,再做修改,而且要保证修改成功,一般都是在setFrame:中设置
*/
- (void)setFrame:(CGRect)frame // 要彻底的修改 控件的尺寸,要再setFrame的基础上去改。
{
frame.size.width += HWMargin; // 在传来的 宽度 基础上再加 HWMargin; 要在 [super setFrame:frame];之前去加;
// frame.origin.x += 10;
// frame.origin.y += 20;
[super setFrame:frame];
}
- (void)layoutSubviews
{
[super layoutSubviews];
// 如果仅仅是调整按钮内部titleLabel和imageView的位置,那么在layoutSubviews中单独设置位置即可
// 1.计算titleLabel的frame
self.titleLabel.x = self.imageView.x;
// 2.计算imageView的frame; imageView 里面放得是那个箭头图片
self.imageView.x = CGRectGetMaxX(self.titleLabel.frame) + HWMargin;
}
- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
[super setTitle:title forState:state];
// 只要修改了文字,就让按钮重新计算自己的尺寸
[self sizeToFit];
}
- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
[super setImage:image forState:state];
// 只要修改了图片,就让按钮重新计算自己的尺寸
[self sizeToFit];
}
@end
#define HWMargin 5
@implementation HWTitleButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont boldSystemFontOfSize:17];
[self setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateSelected];
}
return self;
}
// 目的:想在系统计算和设置完按钮的尺寸后,再修改一下尺寸
/**
* 重写setFrame:方法的目的:拦截设置按钮尺寸的过程
* 如果想在系统设置完控件的尺寸后,再做修改,而且要保证修改成功,一般都是在setFrame:中设置
*/
- (void)setFrame:(CGRect)frame // 要彻底的修改 控件的尺寸,要再setFrame的基础上去改。
{
frame.size.width += HWMargin; // 在传来的 宽度 基础上再加 HWMargin; 要在 [super setFrame:frame];之前去加;
// frame.origin.x += 10;
// frame.origin.y += 20;
[super setFrame:frame];
}
- (void)layoutSubviews
{
[super layoutSubviews];
// 如果仅仅是调整按钮内部titleLabel和imageView的位置,那么在layoutSubviews中单独设置位置即可
// 1.计算titleLabel的frame
self.titleLabel.x = self.imageView.x;
// 2.计算imageView的frame; imageView 里面放得是那个箭头图片
self.imageView.x = CGRectGetMaxX(self.titleLabel.frame) + HWMargin;
}
- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
[super setTitle:title forState:state];
// 只要修改了文字,就让按钮重新计算自己的尺寸
[self sizeToFit];
}
- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
[super setImage:image forState:state];
// 只要修改了图片,就让按钮重新计算自己的尺寸
[self sizeToFit];
}
@end
相关文章推荐
- Codeforces--598A--Tricky Sum(数学)
- 如何让域中的计算机开机自动映射网络上的共享文件夹
- kidd风的IOS日志之Source Tree的使用教程
- 2013551813 张燕燕 第二周作业
- iOS之Runtime
- HighCharts 详细使用及API文档说明
- 为AppDelegate分层的面向服务架构的 SOAComponentAppDelegate
- “1010”问卷调查分析结果及心得体会
- CODE[VS] 1079 回家
- 送给前线码农的话 - 大牛们的经典语录
- 剑指offer-连续子数组的最大和----编程之美-连续子数组的最大和
- CentOs6.5中安装和配置vsftp简明教程
- xUtils更新到3.0后的基本使用规则
- 全虚拟化和半虚拟化的区别 cpu的ring0 ring1又是什么概念?
- JVM梳理
- 从开发者角度解析 Android N 新特性!
- 从开发者角度解析 Android N 新特性!
- shell简介
- Codeforces--596A--Wilbur and Swimming Pool(数学)
- lua-循环