开发者

Customized navigation bar hides buttons

开发者 https://www.devze.com 2023-03-04 16:36 出处:网络
Hope you can help with this one too... I wanted to customize my navigation bar by adding custom background picture. I found out perfect but a bit complicated method called:NavigationSwizzle. In previo

Hope you can help with this one too... I wanted to customize my navigation bar by adding custom background picture. I found out perfect but a bit complicated method called:NavigationSwizzle. In previous project I worked without any serious problems, but now I'm stuck...

From my AppDelegate I am calling this static method:

@implementation SCAppUtils
+ (void)customizeNavigationController:(UINavigationController *)navController
{
//Customizing navigation BAR
UINavigationBar *navBar = [navController navigationBar];
[navBar setTintColor:kSCNavBarColor];

//Customizing TOOLBAR
[navController setToolbarHidden:NO animated:YES];

UIToolbar *toolBar = [navController toolbar];

UIImageView *imageView = (UIImageView *)[navBar viewWithTag:kSCNavBarImageTag];
UIImageView *imageView2 = (UIImageView *)[toolBar viewWithTag:kSCNavBarImageTag];

if (imageView == nil)
{
    Utilities *utilities = [[Utilities alloc]init];

    UIImage *img = [UIImage imageNamed:@"nav-bar-background.png"];
    UIImage *img2 = [UIImage imageNamed:@"toolbar-background.png"];

    imageView = [[UIImageView alloc] initWithImage:img];
    imageView2 = [[UIImageView alloc] initWithImage:img2];  

    [imageView setTag:kSCNavBarImageTag];
    [imageView2 setTag:kSCNavBarImageTag];

    [navBar insertSubview:imageView atIndex:0];
    [toolBar insertSubview:imageView2 atIndex:0];   

    [imageView release];
    [imageView2 release];
    [utilities release];

}

}

In rootNavigationController after calling second view controller by pushing it on the stack my right button, self.title, activityIndicator don't show. If I comment or set atIndex:-1 in line [navBar insertSubview:imageView atIndex:0]; then buttons show, but my customized background is gone and I get regular iPhone navigational tab.

In didSelectRowAtIndex of rootNavigationController I have:

//ADD BACK BUTTON TO VIEW
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton; 

//Home Button color
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:166.0 / 255 green:179.0 / 255 blue:191.0 / 255 alph开发者_如何学运维a:1.0];

//PUSH TO NEXT VIEW
[self.navigationController pushViewController:detailViewController animated:YES];

Thank you in advance...

If you need more code or explanation, please don't hesitate to ask me...

Best regards, Luka

EDIT:

Ok, I can make this question much simpler. I have a custom background image in my navigation bar. This image is obviously covering my

self.navigationItem.title = @"TITLE";

If I put alpha to: 0.3 of imageView that is in the background I can see my title:

imageView = [[UIImageView alloc] initWithImage:image];
[imageView setAlpha:0.3];
[imageView setTag:kSCNavBarImageTag];
[navBar insertSubview:imageView atIndex:0];

So the question is how can I send my navigationItem.title to front? Is it a UIView class descendent?

EDIT 2: Really weird thing is that title is visible (above background image) in the rootController but after pushing another viewController title goes behind te background imageView ?!


I honestly think you forgot to add this code in the main.m. You main.m should look something like this:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "SCClassUtils.h"

int main(int argc, char *argv[])
{
    [SCClassUtils swizzleSelector:@selector(insertSubview:atIndex:)
                          ofClass:[UINavigationBar class]
                     withSelector:@selector(scInsertSubview:atIndex:)];
    [SCClassUtils swizzleSelector:@selector(sendSubviewToBack:)
                          ofClass:[UINavigationBar class]
                     withSelector:@selector(scSendSubviewToBack:)];

    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}


You might want to try this to customize your Navigationbar instead. I use it in my apps and it works like a charm

UIImage *theImage = [UIImage imageNamed:@"barImage.png"];
[self.navigationController.navigationBar insertSubview:[[[UIImageView alloc] initWithImage:theImage] autorelease] atIndex:0];

See if that fixes your issues with the buttons not appearing.


I found a solituion for my work. Create a view (viewBackground) with all the images and colors that conform the navigation bar and then y convert it in a image and use it like a background

UIGraphicsBeginImageContextWithOptions(viewBackground.bounds.size, viewBackground.opaque, 0.0);
[viewBackground.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[[UINavigationBar appearance]            setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号