简体   繁体   中英

Set titleview for all navigationcontroller and navigationitem

[[[UINavigationBar appearance] setTitleView:button]

我试图使用上面的代码行将标题视图设置为所有导航项目的按钮,但是它不起作用。如何通过在appdelegate中编写小代码来设置项目中所有导航栏的标题视图?

Customizing the Appearance of a Navigation Bar

it is alright to modify the barStyle, tintColor, and translucent properties, but you must never directly change UIView-level properties such as the frame, bounds, alpha, or hidden properties directly.

For more detail you can follow apple doc UINavigationBar Class Reference

Edit ->

I solved your query

[[UINavigationBar appearance] addSubview:yourView];  

Other Navigation Related query

try this dude...

//put whatever object in this view..for example button that you want to put...
UIView* ctrl = [[UIView alloc] initWithFrame:navController.navigationBar.bounds];
ctrl.backgroundColor = [UIColor yellowColor];
ctrl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[navController.navigationBar addSubview:ctrl];

let me know it is working or not!!!!

Happy Coding!!!

This is how you create a navigation controller in addDelegate and set a title to it, you have to add the following code to didFinishLaunchingWithOptions method. Notice that you need to set a root view for the viewController which will the viewController that will be displayed inside the navigationController.

  yourViewController *mainVC = [[yourViewController alloc]init];
  UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:mainVC];
  navigationController1.title = @"title";
[window addSubview:navigationController1.view];

If you use iOS4, you can override drawRect

@implementation UINavigationBar (UINavigationBarCategory)

-(void)drawRect:(CGRect)rect
{

    UIImageView *itleImageView = //create object
    [self addSubView:titleImageView];
     //set title view in navigation bar
}
@end

iOS 5,

if ([[UIDevice currentDevice].systemVersion floatValue] >= 5.0) {
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){

   UIImageView *itleImageView = //create object
   [self.navigationController.navigationBar addSubView:titleImageView];
    }
} 

i guess this is a right, as i don't have implemented.

I wanted to add a logo in every NavigationController, so I made a subclass of UINavigationController and used this in the viewDidLoad -Method

UIImage *logo =[UIImage imageNamed:@"logo"];
CGFloat navWidth = self.navigationBar.frame.size.width;
CGFloat navHeight = self.navigationBar.frame.size.height;

UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake(navWidth-logo.size.width - 5,
                                                                      (navHeight - logo.size.height) / 2,
                                                                      logo.size.width,
                                                                      logo.size.height)];

logoView.image = logo;

[self.navigationBar addSubview:logoView];

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