美文网首页GXiOS
iOS开发--自定义UIButton

iOS开发--自定义UIButton

作者: Caesar_62dd | 来源:发表于2019-05-06 21:30 被阅读3次

自定义UIButton时,新建一个类GXButton,继承于UIButton.
初始化:UIButtonTypeCustom

    GXButton *btn = [GXButton buttonWithType:UIButtonTypeCustom];

重写两个方法之一都可以

#pragma mark - 方式一
/**
 *  重写两个方法: 不要调用super,就是要重写掉
 *  contentRect: 内容的尺寸,内容包括(imageView和label)
 */

- (CGRect)titleRectForContentRect:(CGRect)contentRect{
    return CGRectMake(0, 0, 100, 70);
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
    return CGRectMake(100, 0, 70, 70);
}

#pragma mark - 方式二
- (void)layoutSubviews{
    [super layoutSubviews];
        // 设置子控件的位置
    self.titleLabel.frame = CGRectMake(0, 0, 100, 70);
    self.imageView.frame = CGRectMake(100, 0, 70, 70);
}

相关文章

网友评论

    本文标题:iOS开发--自定义UIButton

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