不清楚是怎么调用的,图片和标题的位置是怎么传递的。

来源:2-23 自定义UIButton(上)

慕粉1472629313

2018-11-29 15:03:06

自定义button页面

#import "City.h"


@implementation City

//构造方法

-(id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        self.imageView.contentMode = UIViewContentModeScaleAspectFit;

    }

    return self;

}


-(CGRect)titleRectForContentRect:(CGRect)contentRect{

    CGFloat x = 0;

    CGFloat y = 0;

    CGFloat w = 50;

    CGFloat h = 44;

    return CGRectMake(x, y, w, h);

}

-(CGRect)imageRectForContentRect:(CGRect)contentRect{

    CGFloat w = 15;

    CGFloat h = 15;

    CGFloat x = 50;

    CGFloat y = (44-15)/2;

    

    return CGRectMake(x, y, w, h);

}


@end

另外一个页面导入自定义button的头文件  

City *city = [[City alloc]initWithFrame:CGRectMake(0, 0, 65, 44)];

    [city addTarget:self action:@selector(selectCityAction) forControlEvents:UIControlEventTouchUpInside];

    [city setTitle:@"北京" forState:UIControlStateNormal];

    [city setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [city setImage:[UIImage imageNamed:@"arrow.png"] forState:UIControlStateNormal];

    

    UIBarButtonItem *bbi_selectCity = [[UIBarButtonItem alloc]initWithCustomView:city];

    self.navigationItem.rightBarButtonItem = bbi_selectCity;


写回答

1回答

Tender10

2018-11-29

title和image和frame都是一个道理,都是重写父类的方法,去实现自己想要的一个结果,具体底层怎么转换,怎么操作的,是不需要去考虑的。如果自定义按钮,那就重写父类的方法,去实现自己要想要的结果。

0

0 学习 · 1878 问题

查看课程