2012-05-01 29 views
8

Sự cố khá dễ hiểu với hình ảnh. Tôi có một UINavigationController cho phép người dùng chuyển đổi giữa các chế độ xem.NavigationBar không hiển thị thích hợp khi pushViewController

Quan điểm thứ nhất chứa một thanh tìm kiếm và một cái nhìn bảng như sau: enter image description here

Việc thứ hai là một cái nhìn cơ bản trong đó thông tin về các tế bào đang hiển thị

Khi tôi bấm vào thanh tìm kiếm, điều hướng bộ điều khiển bị ẩn và thanh tìm kiếm hiện ở trên cùng.

Bây giờ, nếu tôi bấm vào một ô, nó đi với quan điểm thứ hai, nhưng thanh điều hướng là lần đầu tiên ẩn như dưới đây:

enter image description here

Và sau đó, nó tự động xuất hiện như thế:

enter image description here

tôi đã thử một vài thứ như hiển thị thanh điều hướng trước khi đẩy bộ điều khiển xem tiếp theo nhưng nó là khá xấu xí .. có ai biết làm thế nào để có hiển thị thanh điều hướng trực tiếp trên chế độ xem thứ hai (như trong ứng dụng liên hệ) không?

[UPDATE]: Mã

AppDelegate.m (Tôi đang nói về navigationcontroller2)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 
    SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 

    viewController1.managedObjectContext = [self managedObjectContext]; 
    viewController2.managedObjectContext = [self managedObjectContext]; 
    viewController1.viewController2 = viewController2; 

    UINavigationController *navigationcontroller1 = [[UINavigationController alloc] initWithRootViewController:viewController1]; 
    [navigationcontroller1.navigationBar setTintColor:[UIColor lightGrayColor]]; 
    UINavigationController *navigationcontroller2 = [[UINavigationController alloc] initWithRootViewController:viewController2]; 
    [navigationcontroller2.navigationBar setTintColor:[UIColor lightGrayColor]]; 

    self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationcontroller1, navigationcontroller2, nil]; 

    self.window.rootViewController = self.tabBarController; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 

FirstView.m

- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar { 
    [self.navigationController setNavigationBarHidden:YES animated:YES]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (!noResultsToDisplay) { 
     PinDetailsViewController *pinDetailsViewController = [[PinDetailsViewController alloc] initWithNibName:@"PinDetailsViewController" bundle:nil]; 
     NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath]; 
     Pin *pin = (Pin *) managedObject; 

     [self.navigationItem setTitle:@"Pins"]; 
     [self.navigationController pushViewController:pinDetailsViewController animated:YES]; 
     [pinDetailsViewController updateWithPin:pin]; 
    } 
} 

Nếu bạn cần bất cứ điều gì khác, chỉ cần hỏi nhưng tôi nghĩ rằng đó là tất cả ở đó.

+0

Vui lòng đăng một số mã. Nếu không thì rất khó để xem vấn đề ở đâu. – MishieMoo

Trả lời

14

Cố gắng sử dụng mã này trong từng chế độ xem.

- (void) viewWillAppear:(BOOL)animated 
{ 

    [self.navigationController setNavigationBarHidden:NO animated:animated]; 

} 


- (void) viewWillDisappear:(BOOL)animated 
{ 
    [self.navigationController setNavigationBarHidden:YES animated:animated]; 

} 
+2

Tôi đã thay đổi toàn bộ bố cục ứng dụng của mình nên tôi không sử dụng nó nữa, nhưng nó hoạt động như một nét duyên dáng. Hy vọng nó sẽ giúp người khác. Cảm ơn bạn. –

+0

Nó giúp tôi khỏi bị đau đầu vào tháng 12 năm 2015! Cảm ơn bạn. – oyalhi

1

Trước khi bạn đẩy điều khiển xem mới, bạn nên thôi ẩn thanh điều hướng:

[self.navigationController setNavigationBarHidden:NO animated:YES];

+0

Nếu bạn đọc bài đăng của tôi một lần nữa, bạn sẽ nhận thấy tôi đã thử điều đó. Nó hoạt động nhưng nó xấu xí. Tôi muốn có hành vi tương tự như trong ứng dụng Liên hệ. –

1

Tôi đã có một vấn đề tương tự với vị trí của thanh điều hướng của tôi. Mine đã được di chuyển lên phía sau thanh trạng thái, và tôi cố định vấn đề bằng cách tự thiết lập các khung navbar:

-(void)adjustNavBarOrigin 
{ 
    CGRect r = self.navigationController.navigationBar.frame; 
    r.origin = CGPointMake(0, 20); // 20 is the height of the status bar 
    self.navigationController.navigationBar.frame = r; 
} 

tôi phải gọi phương thức này ở một số nơi, trong đó có viewWillAppear: và didRotateFromInterfaceOrientation :, nhưng nó làm việc a treat :)

+0

Tôi đã thử nó nhưng nó không giải quyết được vấn đề. –

+0

Hoạt động hoàn hảo của nó; cảm ơn. Đã lưu rất nhiều thời gian – msmq

0

Ẩn UINavigationBar có thể làm phiền các thuộc tính đôi khi. Hãy thử sử dụng thuộc tính alpha thay vì hidden.

Các vấn đề liên quan