iOS多个控制器,如何仅改变某一个控制器view上导航栏的隐藏或透明度

meiyouren
  • 2
新手上路,请多包涵

隐藏了一个控制器的导航栏,其余都变隐藏了。 。

回复
阅读 3.2k
1 个回答

下面的这段代码是让整个ViewController都成为半透明的(也可以设置成不透明),没有导航栏。当从 Controller A 跳转到 Controller B 时,从效果看上去,B就像一个View一样,盖在了A上面。

B

// Present transparent UIViewController over UIViewController
// http://stackoverflow.com/questions/11236367/display-clearcolor-uiviewcontroller-over-uiviewcontroller
// vc.viewDidAppear not getting called, caused by pushViewController:vc, you should not both call presentViewController and pushViewController.
// http://stackoverflow.com/questions/14274706/presentviewcontroller-and-viewdidappear-not-getting-called-in-ios5-1-6
// https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

// @didPresentingVC : the view controller that did the presenting
+ (void)presentedFromViewController:(UIViewController *)didPresentingVC {
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:kStoryboardName bundle:nil];
    //@presentedVC: the view controller that was presented
    // kStroyboardIdB 定义在storyboard, Identity Inspector,
    UIViewController *presentedVC = [mainStoryboard instantiateViewControllerWithIdentifier:kStroyboardIdB];
    // 清除背景色
    presentedVC.view.backgroundColor = [UIColor clearColor];
    // 设置透明度
    presentedVC.view.alpha = 0.5;
    // fix bug that view not transparent on iOS version greater than 8.0 http://stackoverflow.com/a/26387371
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        presentedVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    } else {
        didPresentingVC.modalPresentationStyle = UIModalPresentationCurrentContext;
        didPresentingVC.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
    }
    [didPresentingVC presentViewController:presentedVC animated:YES completion:nil];
}

然后在另一个ViewController A中调用这个方法,就调出了 Controller B:

A

- (void)OnClick {
    [B presentedFromViewController:self];
}
宣传栏