美文网首页
自定义UIBotton,图片在上文字在下

自定义UIBotton,图片在上文字在下

作者: xiaomayi2012 | 来源:发表于2016-12-05 15:32 被阅读15次

    1、新建一个类继承至UIBotton,然后在.m文件中重写layoutSubviews方法,(使用时,只需要把该类通过import 引入,然后继承自该类即可),如下:
    xxxx.m

    @implementation xxxxx
    -(void)setup{
    //设置图片下方文字居中
    self.titleLabel.textAlignment=NSTextAlignmentCenter;
    }
    
    -(instancetype)initWithFrame:(CGRect)frame{
      if(self=[super initWithFrame:frame]){
        [self setup];
      }
    
    }
    
    -(void)awakeFromNib{
      [self setup];
    }
    
    
    -(void)layoutSubviews{
      [super layoutSubviews];
      //调整图片
      self.imageView.x=0;
      self.imageView.y=0;
      self.imageView.width=self.width;
      self.imageView.height=self.imageView.width;
      //调整文字
      self.titleLabel.x=0;
      self.titleLabel.y=0;
      self.titleLabel.width=self.width;
      self.titleLabel.height=self.height-self.titleLabel.y;
      }
    @end
    

    相关文章

      网友评论

          本文标题:自定义UIBotton,图片在上文字在下

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