按钮实现文字在下,图片在上

//按钮实现文字在下,图片在上

-(void)initButton:(UIButton*)btn{
   btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
   // 按钮图片和标题总高度
   CGFloat totalHeight = (btn.imageView.frame.size.height + btn.titleLabel.frame.size.height);
   // 设置按钮图片偏移
   [btn setImageEdgeInsets:UIEdgeInsetsMake(-(totalHeight - btn.imageView.frame.size.height), 0.0, 0.0, -btn.titleLabel.frame.size.width)];
   // 设置按钮标题偏移
   [btn setTitleEdgeInsets:UIEdgeInsetsMake(0.0, -btn.imageView.frame.size.width, -(totalHeight - btn.titleLabel.frame.size.height),0.0)];
}

你可能感兴趣的:(按钮实现文字在下,图片在上)