2009-03-24 29 views
32

Tôi có một UITabBarController nơi trình điều khiển chế độ xem mặc định là UINavigationController. Tôi muốn để có thể ẩn UITabBar của UITabBarController khi tôi đẩy một cái nhìn nhất định trong UINavigationController.Ẩn UITabBar khi ấn UIView

Tôi đã thử thêm:

delegate.tabBarController.hidesBottomBarWhenPushed = YES; 

trong UINavigationController tôi trước khi tôi đẩy quan điểm, nhưng điều đó dường như không làm các trick.

Bất kỳ mẹo nào về những việc tôi nên làm hoặc liệu điều đó có thể thực hiện được không? Cảm ơn trước!

Trả lời

94

này là tốt hơn:

viewController.hidesBottomBarWhenPushed = YES; 
[self.navigationController pushViewController:viewController animated:YES]; 

Bạn phải thiết lập hidesBottomBarWhenPushed = YES trên bộ điều khiển bạn sẽ đẩy vào xem ...

+2

Điều này chắc chắn là tốt hơn. Cảm ơn bạn! – givp

+4

Một điều cần nhớ: bạn phải đặt mã trên TRƯỚC KHI đẩy nó vào bộ điều khiển. Đó là ngay sau dòng khởi tạo. Nó sẽ KHÔNG hoạt động nếu bạn đặt nó trong viewDidLoad hoặc nơi tương tự ... – Lukasz

+0

Rất đẹp, đã giải quyết giờ làm việc của tôi! – Rahul

0

Nó chỉ ra rằng nếu bạn đặt chế độ xem hidesBottomBarWhenPushed:YES nó ẩn thanh khi chế độ xem xuất hiện (duh trên phần của tôi). Tôi đã gán nó cho UITabBarController, điều này không có ý nghĩa quá nhiều khi bạn nghĩ về nó.

[self.view hidesBottomBarWhenPushed:YES]; 
[super pushViewController:viewController animated:animated]; 
+4

sai. cả hai phương pháp nên được áp dụng cho bộ điều khiển xem mà bạn sẽ đẩy vào .. – hfossli

+1

Đây là một câu trả lời rất sai. –

2

Đây là cách bạn có được điều này để làm việc :

Trong Application Delegate bạn tạo UITabBarController. Sau đó, bạn tạo một UINavigationController với bộ điều khiển gốc của nó làm bộ điều khiển chế độ xem bạn muốn trong tab cụ thể. Sau đó, chèn UINavigationController vào mảng "viewControllers" của UITabBarController. như vậy:

ViewControllerForTab1 *tab1Controller = [[ViewControllerForTab1 alloc] initWithNibName:@"ViewControllerForTab1"]; 

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tab1Controller]; 

[tab1Controller release]; 


UITabBarController *tabBarController = [[UITabBarController alloc] init]; 
tabBarController.viewControllers = [NSArray arrayWithObjects: navController, nil]; 

[navController release]; 


[self.window addSubView:tabBarController.view]; 

Bằng cách này bạn có thể thiết lập "hidesBottomBarWhenPushed" tài sản để "YES" trong bất kỳ điều khiển xem bên UINavigationController đó và nó sẽ ẩn UITabBar.

Hy vọng điều đó sẽ hữu ích!

1

tôi sẽ cho đây giải pháp của tôi cho điều này:

#define FRAME_HIDDEN CGRectMake(0, 0, 768, 1073) //1073 = 1024 (screen) + 49 (UITabBar) 
#define FRAME_APPEAR CGRectMake(0, 0, 768,1024) 

-(void) setHidden: (BOOL) hidden{ 
    CGRect frame = (hidden)? FRAME_HIDDEN : FRAME_APPEAR; 
    [self.tabBarController.view setFrame:frame]; 
    [self.tabBarController.tabBar setHidden:hidden]; 
} 

cuộc gọi phương pháp 'setHidden', nơi bạn cần đến nó! Tôi sử dụng mẫu này và 'Mẫu Singleton', khi đó các bài đánh giá của tôi có thể ẩn UITabBar trong Superview

+0

Ẩn như vậy không hoạt động. Vâng, nó không hoạt động nhưng nó để lại một số nền đen, nơi tabbar được sử dụng để được, mà không thể truy cập từ quan điểm của tôi. Di chuyển thanh tab vượt ra ngoài phần hiển thị của màn hình làm các trick. Tuy nhiên, điều này không có tác động đến giới hạn của màn hình mà có thể có tác dụng phụ cho các phần khác của ứng dụng. –

+0

Xin lỗi, nhận xét ở trên không đúng và tôi không thể chỉnh sửa thêm nữa. Thay đổi một cách rõ ràng khung của khung nhìn của tabBarController có một số tác động đến việc layoout của subViews so với giải pháp (đối với tôi không đủ) bằng cách sử dụng hidesBottomBarWhenPushed. Tôi vẫn đang điều tra và có thể đăng cập nhật tại đây, trừ khi lỗi xảy ra ở đâu đó trong các tính toán của tôi. –

+0

Vâng, tôi đã quan sát một số tác dụng phụ có thể được quan tâm: Tôi đã nave một cái nhìn nhỏ phụ trong quan điểm của tôi ở dưới cùng của màn hình đầy đủ.Giả định rằng sẽ không có thanh tab và do đó sử dụng không gian mà thanh tab thường chiếm. Tôi đã thêm quan điểm phụ này trong IB. Trong IB, bạn ghép từng khung nhìn vào một cạnh nhiều hơn hoặc trung tâm của màn hình/suberview cho mục đích tự động hóa. Loại này tự động hóa sự thay đổi định hướng. Tuy nhiên, nó đã định vị lại tiểu sử của tôi cùng với thanh tab đến một nơi nào đó bên dưới màn hình. Chỉ cần giữ cho autosizing trong tâm trí. –

3

Tôi đã tìm ra cách để giải quyết vấn đề này, tôi đã gặp phải vấn đề tương tự, nhưng Apple cũng cho chúng tôi biết cách làm điều đó trong mẫu được gọi là: "The Elements" (http://developer.apple.com/library/ios/#samplecode/TheElements/Introduction/Intro.html)

Xem chức năng dưới đây về cách thực hiện, thêm chức năng này vào chức năng init của chế độ xem bạn muốn nhấn!

-(id) init { 
    if(self = [super init]) { 
     self.hidesBottomBarWhenPushed = YES; 
    } 
    return self; 
} 

Nó sẽ tự động ẩn thanh tab như ứng dụng ảnh trên iphone của bạn. Và khi bạn điều hướng trở lại chế độ xem gốc sẽ chỉ hiển thị lại thanh tabbar.

Chúc may mắn

+0

Điều này làm việc tuyệt vời cho tôi. Tốt hơn bất kỳ giải pháp nào khác cho đến nay – rpgmaker

3

Tôi đã thử hầu hết các giải pháp được đề xuất.Cuối cùng không ai trong số họ làm việc cho tôi.

hideTabBarWhenPushed ẩn thanh tab không chỉ cho bộ điều khiển chế độ xem được đẩy tiếp theo mà còn cho tất cả bộ điều khiển chế độ xem được đẩy bên trong. Đối với những người tôi đã muốn điều khiển thanh tab để tái xuất hiện.

Giải pháp của Orafaelreis (xem bên trên) dường như có nhiều nhất. Nhưng nỗ lực của ông chỉ làm việc cho các định hướng chân dung nghiêm ngặt, thậm chí không bị lộn ngược. Vì vậy, tôi đã phải sửa chữa nó. Đây là những gì tôi đã nhận được cuối cùng:

#define kTabBarHeight    49 // This may be different on retina screens. Frankly, I have not yet tried. 

- (void) hideTabBar:(BOOL)hide { 

    // fetch the app delegate 
    AppDelegate   *delegate = [[UIApplication sharedApplication] delegate]; 

    // get the device coordinates 
    CGRect    bounds  = [UIScreen mainScreen].bounds; 
    float    width; 
    float    height; 

    // Apparently the tab bar controller's view works with device coordinates 
    // and not with normal view/sub view coordinates 
    // Therefore the following statement works for all orientations. 
    width     = bounds.size.width; 
    height     = bounds.size.height; 

    if (hide) { 

     // The tab bar should be hidden too. 
     // Otherwise it may flickr up a moment upon rotation or 
     // upon return from detail view controllers. 
     [self.tabBarController.tabBar setHidden:YES]; 

     // Hiding alone is not sufficient. Hiding alone would leave us with an unusable black 
     // bar on the bottom of the size of the tab bar. 
     // We need to enlarge the tab bar controller's view by the height of the tab bar. 
     // Doing so the tab bar, although hidden, appears just beneath the screen. 
     // As the tab bar controller's view works in device coordinations, we need to enlarge 
     // it by the tab bar height in the appropriate direction (height in portrait and width in landscape) 
     // and in reverse/upside down orientation we need to shift the area's origin beyond zero. 
     switch (delegate.tabBarController.interfaceOrientation) { 
      case UIInterfaceOrientationPortrait: 
       // Easy going. Just add the space on the bottom. 
       [self.tabBarController.view setFrame:CGRectMake(0,0,width,height+kTabBarHeight)]; 
       break; 

      case UIInterfaceOrientationPortraitUpsideDown: 
       // The bottom is now up! Add the appropriate space and shift the rect's origin to y = -49 
       [self.tabBarController.view setFrame:CGRectMake(0,-kTabBarHeight,width,height+kTabBarHeight)]; 
       break; 

      case UIInterfaceOrientationLandscapeLeft: 
       // Same as Portrait but add the space to the with but the height 
       [self.tabBarController.view setFrame:CGRectMake(0,0,width+kTabBarHeight,height)]; 
       break; 

      case UIInterfaceOrientationLandscapeRight: 
       // Similar to Upside Down: Add the space and shift the rect. Just use x and with this time 
       [self.tabBarController.view setFrame:CGRectMake(0-kTabBarHeight,0,width+kTabBarHeight,height)]; 
       break; 

      default: 
       break; 
     } 
    } else { 
     // reset everything to its original state. 
     [self.tabBarController.view setFrame:CGRectMake(0,0,width,height)]; 
     [self.tabBarController.tabBar setHidden:NO]; 
    } 

    return; 
} 


- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ 

    // It is important to call this method at all and to call it here and not in willRotateToInterfaceOrientation 
    // Otherwise the tab bar will re-appear. 
    [self hideTabBar:YES]; 

    // You may want to re-arrange any other views according to the new orientation 
    // You could, of course, utilize willRotateToInterfaceOrientation instead for your subViews. 
} 

- (void)viewWillAppear: (BOOL)animated { 

    // In my app I want to hide the status bar and navigation bar too. 
    // You may not want to do that. If so then skip the next two lines. 
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent; 
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; 

    [self hideTabBar: YES]; 

    // You may want to re-arrange your subviews here. 
    // Orientation may have changed while detail view controllers were visible. 
    // This method is called upon return from pushed and pulled view controllers. 

    return; 
} 

- (void)viewWillDisappear: (BOOL)animated {  

    // This method is called while this view controller is pulled 
    // or when a sub view controller is pushed and becomes visible 
    // Therefore the original settings for the tab bar, navigation bar and status bar need to be re-instated 

    [self hideTabBar:NO]; 

    // If you did not change the appearance of the navigation and status bar in viewWillAppear, 
    // then you can skip the next two statements too. 
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack; 
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide]; 

    return; 
} 

Nhận xét nội dòng nên giải thích lý do cho từng câu. Mặc dù, có thể có cách mã hóa thông minh hơn.

Có một tác dụng phụ cùng với việc ẩn thanh trạng thái và thanh điều hướng, mà tôi không muốn ẩn khỏi các bạn. 1. Khi trở về từ bộ điều khiển điều hướng này đến bộ điều khiển chuyển hướng gọi thì thanh trạng thái và thanh điều hướng trên bộ điều khiển gọi chồng chéo cho đến khi thiết bị được xoay một lần hoặc cho đến khi tab liên quan được chọn lại sau khi một tab khác xuất hiện. 2. Khi bộ điều khiển chế độ xem cuộc gọi là chế độ xem bảng và khi thiết bị ở chế độ nằm ngang khi quay lại bảng, thì bảng được hiển thị theo hướng thích hợp cho phong cảnh nhưng nó được bố trí như thể nó là chân dung. Góc trên bên trái là tốt nhưng một số ô bảng cộng với thanh tab được ẩn bên dưới màn hình. Ở phía bên phải có một số không gian trống. Điều này cũng được cố định bằng cách xoay thiết bị lần nữa.

Tôi sẽ cập nhật cho bạn khi tôi tìm thấy giải pháp cho các lỗi nhỏ nhưng khó chịu này.

3

khi làm việc với kịch bản dễ dàng để nó thiết lập view controller mà sẽ ẩn thanh tabbar trên đẩy, trên bộ điều khiển xem nơi chỉ cần chọn hộp kiểm này:
enter image description here

0

trong UIViewController đầu tiên "FirstItemViewController"

@IBAction func pushToControllerAction(sender: AnyObject) { 
    self.hidesBottomBarWhenPushed = true 
    self.performSegueWithIdentifier("nextController", sender: self) 
} 

ở bên cạnh UIViewController "ExampleViewController" `

override func willMoveToParentViewController(parent: UIViewController?) { 
     if parent == nil { 
      var viewControllers = self.navigationController!.viewControllers 
      if ((viewControllers[viewControllers.count - 2]).isKindOfClass(FirstItemViewController.self)) { 
       (viewControllers[viewControllers.count - 2] as! FirstItemViewController).hidesBottomBarWhenPushed = false 
      } 
     } 
} 

Nhìn vào câu trả lời này https://stackoverflow.com/a/36148064/3078925