在UITabBar上选择时更改UIButton backgroundImage

时间:2011-09-25 19:11:05

标签: iphone objective-c ipad uitabbarcontroller

所以我在我的UITabBar中间有一个UIButton来模仿,Instagram的UITabBar 这是代码(代码也可以从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 = 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。我通过代码确实这样做了,但它并没有改变按钮图像。这是为什么?

1 个答案:

答案 0 :(得分:3)

根据UIControlState的Apple文档: 选择控件的状态。 对于许多控件,此状态对行为或外观没有影响。但是其他子类(例如,UISegmentedControl类)可能具有不同的外观,具体取决于其选择的状态。您可以通过选定的属性检索并设置此值。

当您检查选择的属性时,会提到类似的内容。因此,对于UIButton,必须在按钮的Action按钮中显式设置此属性。

至于按钮,必须完成以下操作,

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

在此按钮的操作方法中,请输入以下代码:

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

其中buttonState应该是相应类中的BOOL iVar。在这种情况下,按钮状态切换选择的状态。因此,根据需要设置所选状态的图像。

另请注意,我们正在获取(UIControlStateHighlighted | UIControlStateSelected)的突出显示图像,因为当您选择按钮时,这是一个中间状态。