我想要达到的效果是:
我的应用程序的当前状态是:
这是我的视图控制器的设置。我在导航栏下面放了一个工具栏。然后,我将工具栏的委托设置为导航栏。我已经阅读了几篇关于此的帖子。提供的一个解决方案是:
navigationController?.navigationBar.shadowImage = UIImage();
navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
但是,这会导致导航栏变白并失去效果。所以我从这篇文章(UISegmentedControl below UINavigationbar in iOS 7)得到了以下代码:
@IBOutlet weak var toolbar: UIToolbar!
var hairLine: UIView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
doneButton.enabled = false
for parent in self.navigationController!.navigationBar.subviews {
for childView in parent.subviews {
if childView is UIImageView && childView.bounds.size.width == self.navigationController!.navigationBar.frame.size.width {
hairLine = childView
print(hairLine.frame)
}
}
}
}
func removeHairLine(appearing: Bool) {
var hairLineFrame = hairLine.frame
if appearing {
hairLineFrame.origin.y += toolbar.bounds.size.height
} else {
hairLineFrame.origin.y -= toolbar.bounds.size.height
}
hairLine.frame = hairLineFrame
print(hairLine.frame)
}
override func viewWillAppear(animated: Bool) {
removeHairLine(true)
}
override func viewWillDisappear(animated: Bool) {
removeHairLine(true)
}
但是,此代码会在视图完全加载之前删除细线但在加载视图时会再次显示。任何解决方案?
答案 0 :(得分:2)
我在这个网站上找到了解决方案,但不记得究竟在哪里。
目标-C:
WebUserControl1.ascx.cs
夫特:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.ControlUI.WebUserControl1" %>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Test: 1</asp:ListItem>
<asp:ListItem>Test: 2</asp:ListItem>
<asp:ListItem>Test: 3</asp:ListItem>
<asp:ListItem>Test: 4</asp:ListItem>
<asp:ListItem>Test: 5</asp:ListItem>
</asp:DropDownList>
答案 1 :(得分:0)
试试这个
for parent in self.navigationController!.navigationBar.subviews {
for childView in parent.subviews {
if(childView is UIImageView) {
childView.removeFromSuperview()
}
}
}
我希望这对你有所帮助。
答案 2 :(得分:0)
你可以用这个
self.navigationController?.navigationBar.subviews[0].subviews.filter({$0 is UIImageView})[0].removeFromSuperview()
答案 3 :(得分:0)
我使用这行代码
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "background"), for: .default)
答案 4 :(得分:0)
我没有找到任何好的Swift 3解决方案所以我添加了这个,基于Ivan Bruel answer.他的解决方案是面向协议的,允许在任何视图控制器中隐藏细线只有一行代码而没有子类化
将此代码添加到您的视图模型中:
protocol HideableHairlineViewController {
func hideHairline()
func showHairline()
}
extension HideableHairlineViewController where Self: UIViewController {
func hideHairline() {
findHairline()?.isHidden = true
}
func showHairline() {
findHairline()?.isHidden = false
}
private func findHairline() -> UIImageView? {
return navigationController?.navigationBar.subviews
.flatMap { $0.subviews }
.flatMap { $0 as? UIImageView }
.filter { $0.bounds.size.width == self.navigationController?.navigationBar.bounds.size.width }
.filter { $0.bounds.size.height <= 2 }
.first
}
}
然后确保不需要细线的视图控制器符合HideableHairlineViewController
协议并致电hideHairline()
。
答案 5 :(得分:0)
UIImageView?
的属性private var navigationBarHairLine: UIImageView?
findHairlineImageViewUnderView
函数此功能过滤视图的子视图以查找高度小于或等于1pt的视图。
func findHairlineImageViewUnderView(view: UIView?) -> UIImageView? {
guard let view = view else { return nil }
if view.isKind(of: UIImageView.classForCoder()) && view.bounds.height <= 1 {
return view as? UIImageView
}
for subView in view.subviews {
if let imageView = findHairlineImageViewUnderView(view: subView) {
return imageView
}
}
return nil
}
ViewWillAppear
中调用创建的函数,并传入navigationBar。它将返回发际线视图,然后将其设置为隐藏。override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationBarHairLine = findHairlineImageViewUnderView(view: navigationController?.navigationBar)
navigationBarHairLine?.isHidden = true
}
答案 6 :(得分:0)
您可以继承UINavigationBar的子类,并在初始化程序中设置以下内容(Swift 5):
shadowImage = UIImage()
setBackgroundImage(UIImage(), for: .default) // needed for iOS 10
例如:
class CustomNavigationBar: UINavigationBar {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupViews()
}
private func setupViews() {
shadowImage = UIImage()
setBackgroundImage(UIImage(), for: .default) // needed for iOS 10
}
}