iOS高阶UI相关iOS开发iOS开发资料收集区

iOS自定义UIButton中Image和title的位置

2016-05-31  本文已影响245人  为之则易ing

1、继承UIButton

#import <UIKit/UIKit.h>

@interface MyButton : UIButton

@property(nonatomic,assign) CGRect titleFrame;
@property(nonatomic,assign) CGRect imageFrame;

@end

2、实现父类方法

#import "MyButton.h"

@implementation MyButton{
    BOOL isSetImageFrame;
    BOOL isSetTitleFrame;
}
-(void)setImageFrame:(CGRect)imageFrame{
    isSetImageFrame = YES;
    _imageFrame = imageFrame;
}
-(void)setTitleFrame:(CGRect)titleFrame{
    isSetTitleFrame = YES;
    _titleFrame = titleFrame;
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    if (isSetImageFrame) {
        return self.imageFrame;
    }else{
        return  [super imageRectForContentRect:contentRect];
    }
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    if (isSetTitleFrame) {
        return self.titleFrame;
    }else{
        return [super titleRectForContentRect:contentRect];
    }
}
@end

3、通过titleFrame、imageFrame设置title、Image的位置

上一篇下一篇

猜你喜欢

热点阅读