码迷,mamicode.com
首页 > 其他好文 > 详细

UIButton的图文垂直排列+两则间隔

时间:2015-11-09 19:24:38      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

   

-(void)layoutSubviews {
    [super layoutSubviews];
    /*
        super view frame
     47:317---------------
     |		 |		      |
     |	23:46|imgView     |
     |	 --------------   |
     |	|    |         |  |
     |	|	 46        |  |
     |--|--23 .	Center |  |
     */
//    // Center image
    CGRect rect = self.imageView.frame;
    rect.size.height=20;
    rect.size.width=20;
    //固定住图层高和宽,避免偏移,具体看你项目图片尺寸定
    self.imageView.frame = rect;
    CGPoint center = self.imageView.center;
    CGRect newFrame = [self titleLabel].frame;
    center.x = self.frame.size.width/2;
    center.y = self.frame.size.height/2 - newFrame.size.height/2;
    self.imageView.center = center;
    
    //Center text
    newFrame.origin.x = self.imageView.frame.size.width/2 + _spacing/2;
    //↑文本框的左侧距离为 图标的宽度/2 + 间隔距离/2,因为,默认布局图片在左,文本在右
    newFrame.origin.y = self.imageView.frame.size.height + self.imageView.frame.origin.y + _spacing;
    //↑文本框的上距离为 图标的y坐标+图片本身的高度+间隔距离
    newFrame.size.width = self.frame.size.width;
    self.titleLabel.frame = newFrame;
}

在继承自UIButton类里面重写layoutSubviews方法


UIButton的图文垂直排列+两则间隔

标签:

原文地址:http://my.oschina.net/u/2488509/blog/528158

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!