简体   繁体   中英

UINavigationBar with customized background - how to make rightBarButton visible

I've customized background of navigation bar in my RootViewController (just part of code)

[navBar insertSubview:customBack atIndex:0];

I push detailViewController and add activity indicator as a rightBarButtonItem

UIActivityIndicatorView *actInd = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;

self.activityIndicator = actInd;

[actInd release];

UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:self.activityIndicator];

[self.navigationItem setRightBarButtonItem:barButton];

[barButton release];

The problem is that the indicator is not visible but without customized background it works OK.

You can better change your navigationBar Background I think. Create a subclass of UINavigationBar and add:

- (void)drawRect:(CGRect)rect {
    UIImage * image = [UIImage imageNamed:@"MyNavigationBarBackground.png"];
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(ctx, 1.0, -1.0); // Otherwise the image is drawn upside-down
    CGContextDrawTiledImage(ctx, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
}

I do that and I never add a problem with any button :)

Zoleas has the right idea. If you don't or can't use a subclass, I think your problem is that you are adding your subview at index 0 so it is on top of the other views in the navigation bar, it is probably hiding your buttons.

set rightBarButtonItem with following way

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Exit" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];

Try this and also check the position of background

The activity indicator is there its just not being activated. Try animating it like this:

[actInd startAnimating];

Or if you want to keep it visible set the hideWhenStopped property like this:

actInd.hidesWhenStopped = NO;

Did you try to change the origin from the UIActivityIndicatorView?

Or creating it inside an UIView and add this UIView to the UIBarButtonItem.

I think the problem can be the frame from the element.

By this way you can resolve the problem: Create activity indicator and add in a view1 Now this view1 add or create navigation bar with the help of this view1. Now when view1 is visible automatically you will be able to see the loading indicator. Just wild try on this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM