Michael
Michael

Reputation: 1238

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.

Upvotes: 0

Views: 401

Answers (6)

UPT
UPT

Reputation: 1480

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.

Upvotes: 0

jrturton
jrturton

Reputation: 119292

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.

Upvotes: 1

Zoleas
Zoleas

Reputation: 4879

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 :)

Upvotes: 2

Jonathan Naguin
Jonathan Naguin

Reputation: 14796

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.

Upvotes: 0

ms83
ms83

Reputation: 1814

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;

Upvotes: 0

Naresh
Naresh

Reputation: 181

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

Upvotes: 0

Related Questions