美文网首页
自定义button,图文显示的

自定义button,图文显示的

作者: biny_ios | 来源:发表于2018-08-27 10:19 被阅读5次

我们经常会遇到一种情况,整体是个button,里面出现上图下文字、或者下图上文字、左图又文字之类的情况,这种情况只需要写一个继承自UIButton的类即可:
示范一种上图下文字的情况,其他情况类似

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.imageView.y = 5*kYY;
    self.imageView.centerX = self.width * 0.5;
    
    self.titleLabel.x = 0;
    self.titleLabel.y = self.imageView.bottom + 10*kYY;
   
    self.titleLabel.height = self.height - self.titleLabel.y;
    self.titleLabel.width = self.width;
}

相关文章

网友评论

      本文标题:自定义button,图文显示的

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