在iOS開發(fā)中,使用UIButton設(shè)置title和p_w_picpath,達到tabBarItem的效果,即title在下,p_w_picpath在上:
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),醴陵企業(yè)網(wǎng)站建設(shè),醴陵品牌網(wǎng)站建設(shè),網(wǎng)站定制,醴陵網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,醴陵網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
目前,我發(fā)現(xiàn)有兩種比較好的方法:
方法一,使用UIEdgeInsets
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
[button setFrame:CGRectMake(100, 100, 60, 60)];
[buttonsetBackgroundColor:[UIColorgreenColor]];
UIImage *p_w_picpath = [UIImage p_w_picpathNamed:@"index"];
[buttonsetTitle:@"首頁"forState:UIControlStateNormal];
[buttonsetImage:p_w_picpathforState:UIControlStateNormal];
// 如果不設(shè)置,則默認p_w_picpath在左,title在右,居中顯示
// 如果如下設(shè)置,則title和p_w_picpath重疊居中顯示
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, -p_w_picpath.size.width, 0.0, 0.0);
button.p_w_picpathEdgeInsets =UIEdgeInsetsMake(0.0,0.0,0.0, -
button.titleLabel.bounds.size.width);
// 如果如下設(shè)置,則title向下移動40個單位,在下,p_w_picpath向上移動20個單位,在上,居中顯示
button.titleEdgeInsets = UIEdgeInsetsMake(40.0, -p_w_picpath.size.width, 0.0, 0.0);
button.p_w_picpathEdgeInsets =UIEdgeInsetsMake(-20.0,0.0,0.0, -
button.titleLabel.bounds.size.width);
[self.viewaddSubview:button];
方法二,重寫UIButton的layoutSubviews
- (void)layoutSubviews
{
[superlayoutSubviews];
CGPoint center =self.p_w_picpathView.center;
center.x = self.frame.size.width/2;
center.y =self.p_w_picpathView.frame.size.height/2;
self.p_w_picpathView.center = center;
CGRect frame = [self titleLabel].frame;
frame.origin.x = 0;
frame.origin.y =self.p_w_picpathView.frame.size.height +2;
frame.size.width =self.frame.size.width;
self.titleLabel.frame = frame;
self.titleLabel.textAlignment =NSTextAlignmentCenter;
[self.titleLabelsetFont:[UIFontsystemFontOfSize:10.0f]];
}