Set titleview for all navigationcontroller and navigationitem

試著忘記壹切 提交于 2019-12-09 23:00:12

问题


[[[UINavigationBar appearance] setTitleView:button]

I tried to set title view as button for all navigation item using above line of code but it is not working.How can I set title view for all navigation bars in my project by writing small code in appdelegate ?


回答1:


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




回答2:


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!!!




回答3:


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];



回答4:


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.




回答5:


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];


来源:https://stackoverflow.com/questions/13890062/set-titleview-for-all-navigationcontroller-and-navigationitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!