我有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];
}
答案 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,它将位于导航控制器之上。