美文网首页iOS高阶UI相关iOS开发iOS开发资料收集区
iOS自定义UIButton中Image和title的位置

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

作者: 为之则易ing | 来源:发表于2016-05-31 14:13 被阅读245次

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的位置

相关文章

网友评论

    本文标题:iOS自定义UIButton中Image和title的位置

    本文链接:https://www.haomeiwen.com/subject/yeicdttx.html