2013-10-15 68 views
2

如何才能讓導航欄僅在第一個視圖中隱藏,我有一些代碼會讓我這樣做,但如果我轉到第二個視圖然後回到我的第一個我可以看到導航欄向上滑動,有什麼辦法可以使它不這樣做。這裏是我的代碼。如何在第一個視圖上隱藏導航欄

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:(BOOL)animated]; 
    [self.navigationController setNavigationBarHidden:YES animated:animated]; 
    [super viewWillAppear:animated]; 
} 

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

回答

2

在第一種觀點

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

在第二視圖

-(void)viewDidLoad:(BOOL)animated 
{ 
    [self.navigationController setNavigationBarHidden:NO animated:NO]; 
} 
+0

感謝好工作 – Jacobanks

-1

做這樣的事情:

@interface MyViewController : UIViewController 

@property (nonatomic) BOOL shouldHideNavBar; 

@end 


@implementation MyViewController 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    [self.navigationController setNavigationBarHidden:self.shouldHideNavBar animated:animated]; 
    [super viewWillAppear:animated]; 
    self.shouldHideNavBar = YES; 
} 

@end 

第一次運行,雖然,對於shouldHideNavBar的默認值是NO。在viewWillAppear第一次被調用之後,它被設置爲YES。所以,下次viewWillAppear被調用時,您的導航欄將被隱藏。

1

你可以只有一個實例變量,並跟蹤它是否是第一次出現視圖,即。 BOOL isFirstTime請務必將其設置爲YESviewDidLoad並在viewDidAppear傳遞給setNavigationBarHidden:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    isFirstTime = YES; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:(BOOL)animated]; 
    [self.navigationController setNavigationBarHidden:isFirstTime animated:animated]; 
    [super viewWillAppear:animated]; 
    isFirstTime = NO; 
}