2016-02-23 28 views
1

我試圖實現的是讓中間選項卡圖像在不同的分辨率(i5,i6,i6 + ...)上展開,這樣有5個選項卡,總是屏幕寬度的1/5。如何將標籤欄中的圖像設置爲全幅,使用resizableImageWithCapInsets

此代碼沒有拉伸上中間片的整個寬度:

UITabBarItem *timelineTabBarItem = [[UITabBarItem alloc] initWithTitle:@"Middle" 
                   image:[[[UIImage imageNamed:@"tab3-on"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 31, 0, 32)] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] 
                 selectedImage:[[[UIImage imageNamed:@"tab3-off"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 31, 0, 32)] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]]; 
timelineViewController.tabBarItem = timelineTabBarItem; 

在I6 + I看到在標籤的中心的圖像。

我很想念/如何正確填寫標籤圖片?

更新:

這裏是預覽圖像:http://i.imgur.com/9IKISIy.png

綠色是應該覆蓋的深藍色區域的原始圖像。

回答

0

試試這個,

UIImageView imgBgView = [[UIImageView alloc] initWithFrame:CGRectMake(itemWidth*itemIndex,0,itemWidth,self.tabBarViewController.tabBar.frame.size.height)]; 
imgBgView.image = [[UIImage imageNamed:@"tab_icon_checkin_inactive.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0]; 
imgBgView.contentMode = UIViewContentModeScaleAspectFit; 
[self.tabBar.tabBar insertSubview:imgBgView atIndex:0]; 

這裏itemWidth是一個標籤的寬度得到它用,

float itemWidth = [[UIScreen mainScreen] bounds].size.width/(float)(self.tabBarViewController.tabBar.items.count); 

它將在的TabBar中間選項卡中設置的圖像。

希望它會幫助你。

相關問題