2
[[[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 ?

Shubham Sharma
  • 499
  • 4
  • 9
  • 21

5 Answers5

6

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

Community
  • 1
  • 1
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
0

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

NiravPatel
  • 3,260
  • 2
  • 21
  • 31
  • I do not have any access to the navigation controller in app delegate nor I want.I just want that whenever navigation bar appears in the project then title view automatically set to what I wrote in the app delegate file. – Shubham Sharma Dec 15 '12 at 07:03
0

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];
coder
  • 5,200
  • 3
  • 20
  • 45
  • I do not want to create any navigation controller in the app delegate file.I just want that whenever navigation bar appears in the project then title view automatically set to what I wrote in the app delegate file. – Shubham Sharma Dec 15 '12 at 07:02
0

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.

imDeveloper
  • 804
  • 1
  • 8
  • 16
0

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];
Carmen
  • 6,177
  • 1
  • 35
  • 40