UIButton的圖文垂直排列+兩則間隔

   

-(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方法佈局

相關文章
相關標籤/搜索