在 UITabBar 上选择时更改 UIButton 背景图像

发布于 2024-12-06 19:47:01 字数 2396 浏览 2 评论 0原文

所以我在 UITabBar 中间有一个 UIButton 来模仿 Instagram 的 UITabBar 这是代码(代码也可以从 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 示例,按中间按钮将使其突出显示。现在我想要的是在按下按钮时保持突出显示,换句话说,我想在选择按钮状态时更改按钮的背景图像。我确实通过代码做到了这一点,但它并没有改变按钮图像。这是为什么呢?

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 = 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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不念旧人 2024-12-13 19:47:02

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

当您检查 selected 属性时,会提到类似的事情。因此,对于 UIButton,必须在按钮的操作按钮中显式设置此属性。

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

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

并且在该按钮的操作方法中,有以下代码:

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文