![](/img/trans.png)
[英]How to stretch backgroundimage in UINavigationBar and UITabBar when autoresized
[英]Changing UIButton backgroundImage when selected on a UITabBar
因此,為了模仿instagram的UITabBar,我在UITabBar的中間有一個UIButton,這是代碼(也可以從github 此處獲取代碼)。
@interface BaseViewController : UITabBarController
{
}
// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*)title image:(UIImage*)image;
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage;
@end
#import "BaseViewController.h"
@implementation BaseViewController
// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image
{
UIViewController* viewController = [[[UIViewController alloc] init] autorelease];
viewController.tabBarItem = [[[UITabBarItem alloc] initWithTitle:title image:image tag:0] autorelease];
return viewController;
}
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
[button setBackgroundImage:highlightImage forState:UIControlStateSelected];
CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
CGPoint center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[self.view addSubview:button];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
如果您派生上面的github鏈接並運行DailyBoothViewController示例,則按下中間按鈕將其突出顯示。 現在,我想要的是在按下按鈕時使高光停留,換句話說,我想在選擇按鈕狀態時更改按鈕的backgroundImage。 我確實是通過代碼完成的,但是它並沒有改變按鈕的圖像。 為什么是這樣?
根據Apple文檔的UIControlState:控件的選定狀態。 對於許多控件,此狀態對行為或外觀沒有影響 。 但是其他子類(例如,UISegmentedControl類)可能會根據其選擇狀態而具有不同的外觀。 您可以通過選定的屬性檢索和設置此值。
當您檢查選定的屬性時,也會提到類似的情況。 因此,對於UIButton,必須在按鈕的“操作”按鈕中顯式設置此屬性。
至於按鈕,必須完成以下操作:
[button setBackgroundImage:highlightImage forState:(UIControlStateHighlighted|UIControlStateSelected)];
[button setBackgroundImage:highlightImage forState:UIControlStateSelected];
在此按鈕的操作方法中,具有以下代碼:
buttonState = !buttonState;
[(UIButton *)sender setSelected:buttonState];
其中buttonState應該是對應類中的BOOL iVar。 在這種情況下,按鈕狀態會切換所選狀態。 因此,根據需要設置用於所選狀態的圖像。
還要注意,我們正在獲取(UIControlStateHighlighted | UIControlStateSelected)的突出顯示圖像,因為當您選擇按鈕時,這是一個中間狀態。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.