2011-11-28 11 views
9

Tôi có một thanh công cụ phía trên chế độ xem của mình bằng nút quay lại. Tôi muốn khi lượt xem tải xuất hiện với thanh công cụ bị ẩn và sau đó, với một lần chạm nút xuất hiện hoạt ảnh.iPhone: Ẩn/hiển thị Thanh công cụ

--Chỉnh sửa-- Tôi hiện không sử dụng Trình điều khiển điều hướng.

+2

'[self.navigationController setToolbarHidden: YES];' – Kjuly

+0

UIView có một tài sản [ẩn] (http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/ UIView.html # // apple_ref/occ/instp/UIView/hidden) – beryllium

+0

Tôi không chắc tại sao câu hỏi này được bỏ phiếu cho -1. Tôi đã bình chọn nó trở lại kể từ khi tôi đang cố gắng điều tương tự với không có NavigationController (mặc dù đó nên được đề cập trong câu hỏi.) –

Trả lời

7

Với thông tin mới - mà không có UINavigationController - những thứ khác nhau. Dưới đây là các bit có liên quan từ mã của tôi ...

Tạo thanh nav và thêm nó vào xem của bạn:

// Create the navigation bar 

self.navBar = [[UINavigationBar alloc] init]; 
[self.view addSubview:self.navBar]; 

Lay nó ra ..

- (CGRect)frameForOrientation:(UIInterfaceOrientation)theOrientation 
{ 
    UIScreen *screen = [UIScreen mainScreen]; 
    CGRect fullScreenRect = screen.bounds;  // always implicitly in Portrait orientation. 
    CGRect appFrame = screen.applicationFrame; 

    // Find status bar height by checking which dimension of the applicationFrame is narrower than screen bounds. 
    // Little bit ugly looking, but it'll still work even if they change the status bar height in future. 
    float statusBarHeight = MAX((fullScreenRect.size.width - appFrame.size.width), (fullScreenRect.size.height - appFrame.size.height)); 

    // Initially assume portrait orientation. 
    float width = fullScreenRect.size.width; 
    float height = fullScreenRect.size.height; 

    // Correct for orientation. 
    if (UIInterfaceOrientationIsLandscape(theOrientation)) { 
     width = fullScreenRect.size.height; 
     height = fullScreenRect.size.width; 
    } 

    // Account for status bar, which always subtracts from the height (since it's always at the top of the screen). 
    height -= statusBarHeight; 

    return CGRectMake(0, statusBarHeight, width, height); 
} 

- (CGSize)viewSizeForOrientation:(UIInterfaceOrientation)theOrientation 
{ 
    CGRect frame = [self frameForOrientation:theOrientation]; 
    return CGSizeMake(frame.size.width, frame.size.height); 
} 

- (void)layoutSubviewsForInterfaceOrientation:(UIInterfaceOrientation)theOrientation withAnimation:(BOOL)animate 
{ 
    CGSize fullSize = [self viewSizeForOrientation:theOrientation]; 
    float width = fullSize.width; 
    float height = fullSize.height; 

    CGRect newFrame = CGRectMake(0, 0, width, height); 
    SubViewController *controller; 
    UIView *theView; 

    // Place the navigation bar 

    CGRect navBarFrame = newFrame; 
    navBarFrame.size.height = NAVBARHEIGHT; 
    self.navBar.frame = navBarFrame; 
} 

Tạo một chức năng để người nó/giấu nó:

- (void)showNavigationBar:(BOOL)show 
{ 
    if (show == YES && self.navBar.hidden == YES) { 

     // Move the frame out of sight 
     CGRect frame = self.navBar.frame; 
     frame.origin.y = -frame.size.height; 
     self.navBar.frame = frame; 

     // Display it nicely 
     self.navBar.hidden = NO; 
     frame.origin.y = 0.0; 
     [self.view bringSubviewToFront:self.navBar]; 

     [UIView animateWithDuration:0.3 
         animations:^(void) { 
          self.navBar.frame = frame; 
         } 
     ]; 
    } 
    else if (show == NO && self.navBar.hidden == NO) { 

     CGRect frame = self.navBar.frame; 

     // Display it nicely 
     frame.origin.y = -frame.size.height; 
     [self.view bringSubviewToFront:self.navBar]; 

     [UIView animateWithDuration:0.3 
         animations:^(void) { 
          self.navBar.frame = frame; 
         } 
         completion:^(BOOL finished) { 
          self.navBar.hidden = YES; 
         } 
     ]; 
    } 
} 

nơi

#define NAVBARHEIGHT 44 
11

Trước quan điểm được trình bày:

[self.navigationController setToolbarHidden:YES]; 

Khi bạn nhấn vào nút:

[self.navigationController setToolbarHidden:NO]; 
+0

Tôi đã đặt thanh công cụ trong bộ điều khiển chế độ xem chứ không phải trong bộ điều khiển điều hướng. Có cách nào để làm cho công việc điều hoặc tôi defenately sử dụng một cotroller chuyển hướng? –

+0

Xem câu trả lời khác của tôi – tarmes

1

Dưới đây là một số mã của tarmes được chuyển đến MonoTouch/C#.

public static RectangleF FrameForOrientation(UIInterfaceOrientation orientation) { 
     var screen = UIScreen.MainScreen; 
     var fullScreenRect = screen.Bounds;  // always implicitly in Portrait orientation. 
     var appFrame = screen.ApplicationFrame; 

     // Find status bar height by checking which dimension of the applicationFrame is narrower than screen bounds. 
     // Little bit ugly looking, but it'll still work even if they change the status bar height in future. 
     var statusBarHeight = Math.Max((fullScreenRect.Width - appFrame.Width), (fullScreenRect.Height- appFrame.Height)); 

     // Initially assume portrait orientation. 
     var width = fullScreenRect.Width; 
     var height = fullScreenRect.Height; 

     // Correct for orientation. 
     if (IsLandscapeOrientation(orientation)) { 
      width = fullScreenRect.Height; 
      height = fullScreenRect.Width; 
     } 

     // Account for status bar, which always subtracts from the height (since it's always at the top of the screen). 
     height -= statusBarHeight; 

     return new RectangleF(0, statusBarHeight, width, height); 
    } 

    public static SizeF SizeForOrientation(UIInterfaceOrientation orientation) { 
     var frame = FrameForOrientation(orientation); 
     return new SizeF(frame.Width, frame.Height); 
    } 


    public static bool IsLandscapeOrientation(UIInterfaceOrientation orientation) { 
     return 
      orientation == UIInterfaceOrientation.LandscapeLeft || 
      orientation == UIInterfaceOrientation.LandscapeRight; 
    }