【问题标题】:How to remove UINavigationController / UINavigationBar background in objective-c?如何在 Objective-c 中删除 UINavigationController / UINavigationBar 背景?
【发布时间】:2017-07-19 09:25:46
【问题描述】:

我有 2 个 ViewController,第一个是 NavigationController 的 rootviewcontroller。我想在第二个视图控制器中删除导航栏的背景,并在顶部显示我的导航项。 怎么做 ?当我弹出第二个视图控制器时,我应该怎么做,导航栏有它的旧背景而不是 secendview 控制器背景?

这是我的代码:

@implementation testView1

- (void)viewDidLoad
{
    [super viewDidLoad];
    [button addTarget:self action:@selector(ClickAction) forControlEvents:UIControlEventTouchUpInside   ];
    self.navigationController.navigationBar.translatesAutoresizingMaskIntoConstraints = false;
    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.title = @"Home";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
}

-(void)ClickAction
{
    testView2 *detail =  [self.storyboard instantiateViewControllerWithIdentifier:@"V2"];
    [self.navigationController pushViewController:detail animated:YES];
}

@implementation testView2

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor lightGrayColor];
    self.title = @"No";
    self.navigationController.navigationBar.barTintColor = nil;
    self.navigationController.navigationBar.backgroundColor = nil;
    self.navigationController.navigationBar.tintColor = nil;
    self.navigationController.view.tintColor = nil;
    self.navigationController.view.backgroundColor = nil;

    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"left-arrow.png"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    backButtonItem.imageInsets = UIEdgeInsetsMake(45, 5, 45, 85);
    self.navigationItem.leftBarButtonItem = backButtonItem;
}

-(void) Back:(id) sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

【问题讨论】:

    标签: ios objective-c uinavigationcontroller uinavigationbar


    【解决方案1】:

    在第一个控制器中

      -(void)viewWillAppear:(BOOL)animated
     {
      self.navigationController.navigationBar.translatesAutoresizingMaskIntoConstraints = false;
    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.title = @"Home";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
      [super viewWillAppear:animated];
     }
    

    在 SecondView 控制器中编写代码

     - (void)viewDidLoad 
     {
      [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                             forBarMetrics:UIBarMetricsDefault];
     self.navigationController.navigationBar.shadowImage = [UIImage new];
     self.navigationController.navigationBar.translucent = YES;
     self.navigationController.navigationBar.tintColor = [UIColor clearColor];
     self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
     }
    

    【讨论】:

    • 谢谢,背景已移除,但点击后退按钮后出现错误“无法为控制器管理的 UINavigationBar 选择布局方法”。
    • 我认为这是您的约束问题。请检查您的约束
    • 是的,我删除了第一行代码,现在没有错误但是弹出后,导航没有背景
    • self.navigationController.navigationBar.backgroundColor 在第一个控制器中赋予颜色
    • 我必须返回背景图像和阴影图像重新设置颜色
    【解决方案2】:

    ViewController.h 文件:

    - (void)showForViewController:(UIViewController*)controller;
    

    ViewController.m 文件:

    - (void)showForViewController:(UIViewController *)controller {
    
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
        {
            self.providesPresentationContextTransitionStyle = YES;
            self.definesPresentationContext = YES;
    
            [self setModalPresentationStyle:UIModalPresentationOverCurrentContext];
        }
        else
        {
            [self setModalPresentationStyle:UIModalPresentationCurrentContext];
            [self.navigationController setModalPresentationStyle:UIModalPresentationCurrentContext];
        }
    
        [controller.navigationController presentViewController:self animated:NO completion:nil];
    }
    

    我正在使用此代码来显示具有清晰背景的当前 ViewController(查看以前的控制器)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      相关资源
      最近更新 更多