美文网首页iOS开发随笔~程序员
一个按钮显示多种样式【图文】

一个按钮显示多种样式【图文】

作者: 世玉茹花 | 来源:发表于2020-05-20 17:26 被阅读0次
1589966418837_EDF129B7-550C-45C5-807A-A7E5DDCEBCB5.png

同一个button 想显示如上多种样式,有的图片跟随文字,有的图片位置固定,有的无图。。。。。

之前试过复杂做法,后来发现个简单的->

继承UIButton 创建Test_Button,增加属性

@property (nonatomic,assign)BOOL isFlag;

.m方法重写下面方法就👌:

- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    
    if (self.isFlag) {
            return CGRectMake(0, 0, contentRect.size.width-25, contentRect.size.height);

    }else
    {
        return self.bounds;
    }
       
}


-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
    if (self.isFlag) {
            return CGRectMake(contentRect.size.width-25, 5, 20, 20);

    }else
    {
        return self.bounds;
    }
}

相关文章

网友评论

    本文标题:一个按钮显示多种样式【图文】

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