如何在ViewController及其NavigationController上添加UIview

时间:2013-10-31 16:29:25

标签: ios iphone objective-c uiview uinavigationcontroller

我有ViewController NavigationController我希望在按下ViewController按钮时在UIView上添加一些带有一些按钮的半透明ViewController,问题是我无法将UIView放在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];
}

4 个答案:

答案 0 :(得分:4)

我回答了类似的问题here

尝试这样的事情:

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

答案 1 :(得分:2)

将其添加到AppDelegate的UIWindow中。

- (void)setProfileImage
{
    [self setOpacityView];

    [ [[UIApplication sharedApplication] delegate].window addSubview:opacityVw];
}

不要忘记更改视图大小:

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

答案 2 :(得分:0)

简单地说:

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

-(void) closeView
{
    [opacityVw removeFromSuperview];
    self.navigationController.navigationBarHidden = NO;
}

答案 3 :(得分:0)

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