码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UIButton 设置图片文字垂直排列

时间:2015-06-18 17:04:51      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

后面经过测试,如果button的文字长度变更,会导致图片位置变化,经过多次修改UIEdgeInsets的值也没有达到期望效果,最终采用集成UIButton类,重写layoutSubviews函数实现,特将成果记录一下,以便后续查阅

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-(void)layoutSubviews {
    [super layoutSubviews];
 
    // Center image
    CGPoint center = self.imageView.center;
    center.x = self.frame.size.width/2;
    center.y = self.imageView.frame.size.height/2;
    self.imageView.center = center;
 
    //Center text
    CGRect newFrame = [self titleLabel].frame;
    newFrame.origin.x = 0;
    newFrame.origin.y = self.imageView.frame.size.height + 5;
    newFrame.size.width = self.frame.size.width;
 
    self.titleLabel.frame = newFrame;
    self.titleLabel.textAlignment = UITextAlignmentCenter;
}

通过重写,重新设置imageView的Center和titleLabel的Frame解决上述问题。

 

iOS UIButton 设置图片文字垂直排列

标签:

原文地址:http://www.cnblogs.com/zhoup/p/4586098.html

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