开发者

Changing UIButton backgroundImage when selected on a UITabBar

So I have a UIButton in the middle of my UITabBar in order to mimic, instagram's UITabBar Here's the code (code can also be taken from github here).

@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 = C开发者_StackOverflow社区GRectMake(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

If you fork the github link above and run the DailyBoothViewController example, pressing the middle button will have it highlighted. Now what I want is that for the highlight to stay when the button is pressed, in other words I want to change the backgroundImage of the button when the state of the button is selected. I did do that via code, however it isn't changing the button image. Why is this?


According to the Apple documentation for UIControlState: Selected state of a control. For many controls, this state has no effect on behavior or appearance. But other subclasses (for example, the UISegmentedControl class) may have different appearance depending on their selected state. You can retrieve and set this value through the selected property.

And when you check the selected property, similar thing is mentioned. Hence for UIButton this property has to be set explicitly in the Action button of the button.

As for the button the following must be done,

    [button setBackgroundImage:highlightImage forState:(UIControlStateHighlighted|UIControlStateSelected)];
    [button setBackgroundImage:highlightImage forState:UIControlStateSelected];

And in the action method of this button, have the following code:

    buttonState = !buttonState;
    [(UIButton *)sender setSelected:buttonState];

Where buttonState should be a BOOL iVar in the corresponding class. In this case, the button state toggles the selected state. Hence the image for the selected state is set as desired.

Also note that we are getting the highlighted image for (UIControlStateHighlighted|UIControlStateSelected), because, when you select the button, this is an intermediate state.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜