【问题标题】:How to add UIview over a ViewController and its NavigationController如何在 ViewController 及其 NavigationController 上添加 UIview
【发布时间】:2013-10-31 16:29:24
【问题描述】:

我有一个ViewController 和一个NavigationController,当我按下ViewController 按钮时,我想在ViewController 上添加一个带有一些按钮的半透明UIView,问题是我不能把@987654325 @超过NavigationBar。我该如何解决这个问题?

这是我的代码(很简单)

-(void)setOpacityView
{
    opacityVw = [[UIView alloc] initWithFrame:self.view.bounds];
    opacityVw.backgroundColor = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];


    WPCustomButton *closeBtn = [[WPCustomButton alloc] initWithFrame:CGRectMake(230, 10, 80, 20)];
    [closeBtn setTitle:@"Close X" forState:UIControlStateNormal];
    [closeBtn setBackgroundColor:[UIColor clearColor]];
    [closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
    [opacityVw addSubview:closeBtn];
}

// ---------------------------------------------------------------------------------------------------------------------

#pragma mark - Button methods

-(void) closeView
{
    [opacityVw removeFromSuperview];
}


-(void)setProfileImage
{
    [self setOpacityView];

    [self.view addSubview:opacityVw];
}

【问题讨论】:

    标签: ios iphone objective-c uiview uinavigationcontroller


    【解决方案1】:

    我回答了类似的问题here

    试试这样的:

    -(void)setProfileImage
    {
        [self setOpacityView];
        [self.navigationController.view addSubview:opacityVw];
    }
    

    【讨论】:

      【解决方案2】:

      改为将其添加到 AppDelegate 的 UIWindow

      - (void)setProfileImage
      {
          [self setOpacityView];
      
          [ [[UIApplication sharedApplication] delegate].window addSubview:opacityVw];
      }
      

      不要忘记更改视图大小:

      opacityVw = [[UIView alloc] initWithFrame:[[[UIApplication sharedApplication] delegate]window].bounds];
      

      【讨论】:

      • 它不起作用。我认为是因为这个 viewController 不是 appDelegate 之后的下一个
      【解决方案3】:

      您可以创建 MainViewController 并将其作为您的 window.rootViewController。将您的 navigationController 添加到此 MainViewController。之后,您将视图添加到您的 mainViewController,它将位于导航控制器的顶部。

      【讨论】:

        【解决方案4】:

        简单点:

        -(void)setProfileImage
        {
            [self setOpacityView];
            self.navigationController.navigationBarHidden = YES;
            [self.view insertSubview:opacityVw aboveSubview:self.view];
        }
        
        -(void) closeView
        {
            [opacityVw removeFromSuperview];
            self.navigationController.navigationBarHidden = NO;
        }
        

        【讨论】:

        • 我已经编辑了我的帖子,因为我阅读您的问题太快了,它应该可以按要求工作
        • 除了他什么也没说隐藏导航栏。他也希望导航栏上的半透明视图。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-06
        • 2017-08-25
        相关资源
        最近更新 更多