Saturday, June 8, 2013

UINavigationController navigationBar example in Objective C (iOS).

UINavigationController navigationBar

The navigation bar managed by the navigation controller. (read-only)

@property(nonatomic, readonly) UINavigationBar *navigationBar

Discussion of [UINavigationController navigationBar]
It is permissible to modify the barStyle or translucent properties of the navigation bar but you must never change its frame, bounds, or alpha values directly. To show or hide the navigation bar, you should always do so through the navigation controller by changing its navigationBarHidden property or calling the setNavigationBarHidden:animated: method.

UINavigationController navigationBar example.
If you really want the navigation bar and you're targeting 3.2 and up I would recommend to attach an UITapGestureRecognizer to the navigationBar.

- (void)viewDidLoad {
    UITapGestureRecognizer* tapRecon = [[UITapGestureRecognizer alloc]
              initWithTarget:self action:@selector(navigationBarDoubleTap:)];
    tapRecon.numberOfTapsRequired = 2;
    [navController.navigationBar addGestureRecognizer:tapRecon];
    [tapRecon release];

- (void)navigationBarDoubleTap:(UIGestureRecognizer*)recognizer {
    [tableView setContentOffset:CGPointMake(0,0) animated:YES];

Example of [UINavigationController navigationBar].
// Initialise the navigation controller with the first view controller as its root view controller
navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

// Add your code here
[navigationController.navigationBar setBarStyle:UIBarStyleBlack];
// I have commented out the set translucent attribute, as it has some interesting effects.  Try uncommenting it :).
// [navigationController.navigationBar setTranslucent:YES];
[navigationController.navigationBar setTintColor:[UIColor redColor]];

// If you wish to hide the navigation controller
// Note: We are able to alternatively hide or show the navigation bar by using
// [self.navigationController setNavigationBarHidden:YES] in our viewWillAppear method
// within the specific UIViewController that we wish to hide/show the navigation bar
[navigationController setNavigationBarHidden:YES];

// Navigation controller has copy of view controller, so release our copy
[rootViewController release];

UINavigationController navigationBar example.
- (void)viewDidLoad
    [super viewDidLoad];
    self.title = @"My Title";
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:255.0/255
                                                                                  green:10.0/255 blue:100.0/255 alpha:1];

- (void)viewDidLoad
   super viewDidLoad];
   self.title = @"My Title";
   self.navigationController.navigationBar.barStyle = UIBarStyleBlack; //스타일 적용
   self.navigationController.navigationBar.translucent = YES; // 반투명 효과 주기

End of UINavigationController navigationBar example article.