Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging UIButton backgroundImage when selected on a UITabBar
    primarykey
    data
    text
    <p>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 <a href="https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar" rel="nofollow">here</a>).</p> <pre><code>@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 &lt; 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 </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload