UIToolBar位于UINavigationController之上

时间:2016-05-07 10:29:47

标签: ios iphone uinavigationcontroller uinavigationbar uitoolbar

如何将UIToolBar移至顶部(坚持UINavigationBar)?

我很长时间都在努力解决这个问题,我尝试过这样的事情:

  • 符合UIToolBarUIToolbarDelegate的自定义(UIBarPosition)positionForBar:(id <UIBarPositioning>)bar被调用 然后我返回UIBarPositionTop,但工具栏停留在底部。
  • 更改工具栏框架:self.navigationController.toolbar.frame = CGRectMake(0, NAV_BAR_Y, self.view.bounds.size.width, NAV_BAR_HEIGHT);
  • 具有此委托功能的自定义UINaviagtionController(UIBarPosition)positionForBar:(id <UIBarPositioning>)bar { return UIBarPositionTop; }

没有任何斗争进展顺利,同样的看法: enter image description here

任何帮助都会很棒。

(我希望导航看起来为Apple App store导航)

2 个答案:

答案 0 :(得分:1)

我知道2个选项。

1)与Move UINavigationController's toolbar to the top to lie underneath navigation bar

相关

可以设置UINavigationController的子类,并在设置该值后更改工具栏的Y轴位置。

import UIKit

private var context = 0

class NavigationController: UINavigationController {
    private var inToolbarFrameChange = false
    var observerBag: [NSKeyValueObservation] = []

    override func awakeFromNib() {
        super.awakeFromNib()
        self.inToolbarFrameChange = false
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        observerBag.append(
            toolbar.observe(\.center, options: .new) { toolbar, _ in
                if !self.inToolbarFrameChange {
                    self.inToolbarFrameChange = true
                    toolbar.frame = CGRect(
                        x: 0,
                        y: self.navigationBar.frame.height + UIApplication.shared.statusBarFrame.height,
                        width: toolbar.frame.width,
                        height: toolbar.frame.height
                    )
                    self.inToolbarFrameChange = false
                }
            }
        )
    }

    override func setToolbarHidden(_ hidden: Bool, animated: Bool) {
        super.setToolbarHidden(hidden, animated: false)

        var rectTB = self.toolbar.frame
        rectTB = .zero
    }
}

2)您可以创建自己的UIToolbar并将其添加到UIViewController的视图中。然后,将约束添加到安全区域的开头,结尾和顶部。

import UIKit

final class ViewController: UIViewController {
    private let toolbar = UIToolbar()
    private let segmentedControl: UISegmentedControl = {
        let control = UISegmentedControl(items: ["Op 1", "Op 2"])
        control.isEnabled = false
        return control
    }()

   override func loadView() {
        super.loadView()
        setupToolbar()
   }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.hideBorderLine()
    }

    private func setupToolbar() {
        let barItem = UIBarButtonItem(customView: segmentedControl)
        toolbar.setItems([barItem], animated: false)
        toolbar.isTranslucent = false
        toolbar.isOpaque = false

        view.addSubview(toolbar)

        toolbar.translatesAutoresizingMaskIntoConstraints = false
        toolbar.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        toolbar.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        toolbar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    }
}

private extension UINavigationBar {

    func showBorderLine() {
        findBorderLine().isHidden = false
    }

    func hideBorderLine() {
        findBorderLine().isHidden = true
    }

    private func findBorderLine() -> UIImageView! {
        return self.subviews
            .flatMap { $0.subviews }
            .compactMap { $0 as? UIImageView }
            .filter { $0.bounds.size.width == self.bounds.size.width }
            .filter { $0.bounds.size.height <= 2 }
            .first
    }
}

答案 1 :(得分:-1)

试试这个解决方案

@interface ViewController () <UIToolbarDelegate>
{
    UIToolbar * lpToolbar;
}
@end

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    lpToolbar = [[UIToolbar alloc] initWithFrame :CGRectZero];
    lpToolbar.delegate = self;
    self.navigationItem.title = @"Title";
}

-(void) viewWillAppear :(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.navigationController.view addSubview :lpToolbar];
    CGRect rFrame = self.navigationController.navigationBar.frame;
    lpToolbar.frame = CGRectMake( 0.0, rFrame.origin.y + rFrame.size.height, rFrame.size.width, 50.0 );
}

-(void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [lpToolbar removeFromSuperview];
}

-(UIBarPosition) positionForBar:(id <UIBarPositioning>)bar
{
    return UIBarPositionTop;
}