简体   繁体   English

具有自定义背景的UINavigationBar-如何使rightBarButton可见

[英]UINavigationBar with customized background - how to make rightBarButton visible

I've customized background of navigation bar in my RootViewController (just part of code) 我已经在RootViewController中自定义了导航栏的背景(只是代码的一部分)

[navBar insertSubview:customBack atIndex:0];

I push detailViewController and add activity indicator as a rightBarButtonItem 我推送detailViewController并将活动指示器添加为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. 我认为您可以更好地更改您的navigationBar背景。 Create a subclass of UINavigationBar and add: 创建UINavigationBar的子类并添加:

- (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. Zoleas有正确的想法。 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. 如果您不使用或不能使用子类,我想您的问题是您要在索引0处添加子视图,因此该子视图位于导航栏中其他视图的顶部,这可能是隐藏了按钮。

set rightBarButtonItem with following way 通过以下方式设置rightBarButtonItem

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Exit" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@“退出”样式:UIBarButtonItemStylePlain目标:自我操作:@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: 或者,如果要使其可见,请设置hideWhenStopped属性,如下所示:

actInd.hidesWhenStopped = NO;

Did you try to change the origin from the UIActivityIndicatorView? 您是否尝试过从UIActivityIndi​​catorView更改原点?

Or creating it inside an UIView and add this UIView to the UIBarButtonItem. 或在UIView中创建它,并将此UIView添加到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. 通过这种方式,您可以解决问题:创建活动指示器并添加到view1中现在,此view1借助此view1添加或创建导航栏。 Now when view1 is visible automatically you will be able to see the loading indicator. 现在,当view1自动可见时,您将能够看到加载指示器。 Just wild try on this. 只是疯狂尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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