创建子类继承自UIButton,在layoutSubviews方法中改变文字和图片的位置就可以了,同理,稍作改变,可以写出文字在上图片在下.本文只给出文字在下图片在上的代码
-(void)layoutSubviews {
[super layoutSubviews]; // image center
CGPoint center;
center.x = self.frame.size.width/;
center.y = self.imageView.frame.size.height/;
self.imageView.center = center; //text
CGRect newFrame = [self titleLabel].frame;
newFrame.origin.x = ;
newFrame.origin.y = self.imageView.frame.size.height + ;
newFrame.size.width = self.frame.size.width; self.titleLabel.frame = newFrame;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
}
效果: