gpt4 book ai didi

ios - 去掉导航栏下的细线

转载 作者:可可西里 更新时间:2023-11-01 01:37:42 25 4
gpt4 key购买 nike

我想要实现的效果是:

enter image description here

我的应用的当前状态是:

enter image description here

这是我的 View Controller 的设置。我在导航栏下面放了一个工具栏。然后,我将工具栏的委托(delegate)设置为导航栏。我已经阅读了几篇关于此的文章。提供的一种解决方案是:

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)
}

然而,这段代码在 View 完全加载之前删除了细线,但是当 View 加载时,它又出现了。有什么解决办法吗?

最佳答案

我在这个网站上找到了解决方案,但不记得确切的位置。

objective-C :

@interface YourViewController () {
UIImageView *navBarHairlineImageView;
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
navBarHairlineImageView.hidden = YES;
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}

swift :

class YourViewController: UIViewController {
var navBarLine: UIImageView?

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navBarLine = findHairlineImageViewUnderView(self.navigationController?.navigationBar)
navBarLine?.hidden = true
}

func findHairlineImageViewUnderView(view: UIView?) -> UIImageView? {
if view.isKindOfClass(UIImageView.classForCoder()) && view.bounds.height <= 1 {
return view as? UIImageView
}
for subview in view.subviews {
if let imgView = findHairlineImageViewUnderView(subview) {
return imgView
}
}
return nil
}
}

关于ios - 去掉导航栏下的细线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34452920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com