ios 如何在导航栏上添加 UIView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14232273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add UIView over navigation bar?
提问by Andrii Tishchenko
I need overlay UINavigationBar
with UIView
like here
我需要覆盖UINavigationBar
有UIView
喜欢这里
Is there a way to do this except using custom UIView with button back as nav bar?
除了使用带有按钮返回的自定义 UIView 作为导航栏之外,有没有办法做到这一点?
回答by Martin Belcher - AtWrk
You can add a subview to the base view of the Application
您可以将子视图添加到应用程序的基本视图
[[[UIApplication sharedApplication] keyWindow] addSubview:vMyCustomUIView];
To make sure it is only shown when your view controller is visible you could add and remove it in the viewDidAppear
and viewDidDisappear
delegate methods. Here is an example that would show a blue box overlapping them.
为了确保它仅在您的视图控制器可见时显示,您可以在viewDidAppear
和viewDidDisappear
委托方法中添加和删除它。这是一个示例,将显示一个与它们重叠的蓝色框。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
vTestView = [[UIView alloc] initWithFrame:CGRectMake(10.0f,
10.0f,
100.0f,
100.0f)];
vTestView.backgroundColor = [UIColor blueColor];
}
-(void)viewDidAppear:(BOOL)animated
{
[[[UIApplication sharedApplication] keyWindow] addSubview:vMyCustomUIView];
}
-(void)viewDidDisappear:(BOOL)animated
{
[vMyCustomUIView removeFromSuperview];
}
回答by delannoyk
Use the method :
使用方法:
- (void)bringSubviewToFront:(UIView *)view
So in your case that will be :
所以在你的情况下,这将是:
[navigationBar addSubview:myRibbon];
[navigationBar bringSubviewToFront:myRibbon];
Also don't forget that with this way, your ribbon won't be showed completely except if you do this :
另外不要忘记,通过这种方式,除非您这样做,否则您的功能区将不会完全显示:
navigationBar.clipsToBounds = NO;
回答by Andrii Tishchenko
I hide NavigationBar and added UIView instead. It looks the same as Navbar. then add more one UIView with red bookmark. Beside it give possibility to animate bookmark on touch separately.
我隐藏了 NavigationBar 并添加了 UIView。它看起来与导航栏相同。然后再添加一个带有红色书签的 UIView。除此之外,还可以单独为触摸书签设置动画。
If I added bookmark this way: [navigationBar addSubview:myRibbon]; it was hidden on half of size
如果我以这种方式添加书签:[navigationBar addSubview:myRibbon]; 它隐藏在一半大小
回答by JP Aquino
Here's a Swift 3 answer
这是 Swift 3 的答案
UIApplication.shared.keyWindow?.addSubview(menuView)
回答by aianLee
Here's how implemented mine, hope this helps..
这是我的实现方式,希望这会有所帮助..
override func viewWillAppear(_ animated: Bool) {
let testView = UIView(frame: .zero)
testView.backgroundColor = .black
testView.layer.cornerRadius = 10
testView.translatesAutoresizingMaskIntoConstraints = false
self.navigationController?.navigationBar.addSubview(testView)
NSLayoutConstraint.activate([
testView.widthAnchor
.constraint(equalToConstant: 50),
testView.heightAnchor
.constraint(equalToConstant: 70),
testView.topAnchor
.constraint(equalTo: (self.navigationController?.navigationBar.topAnchor)!),
testView.trailingAnchor
.constraint(equalTo: (self.navigationController?.navigationBar.trailingAnchor)!, constant: -20)
])
self.customView = testView
}
// i dont need to display the overlay in every page
// so i remove it everytime i navigate to a new page
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.customView.removeFromSuperview()
}