简体   繁体   English

标题下方 NavigationBar 上的 UiSegmentedControl

[英]UiSegmentedControl on NavigationBar below Title

I am new on iOS Development and during my works for an app I am building right now some doubts have appeared to me.我是 iOS 开发的新手,在为我正在构建的应用程序工作期间,我出现了一些疑问。 I am trying to build a Screen that will be compounded by multiple ViewControllers, but on the NavigationBar I would like to have a UiSegmentedControl above the Title, something like a Scope Bar to control the navigation between the children ViewController.我正在尝试构建一个将由多个 ViewController 复合的屏幕,但是在 NavigationBar 上,我希望在标题上方有一个 UiSegmentedControl,类似于范围栏来控制子 ViewController 之间的导航。 I wanted to build something similar to what we have on HealthKit Dashboard:我想构建类似于我们在 HealthKit Dashboard 上的东西:

在此处输入图片说明 . .

What kind of approach do you suggest to do that?你建议用什么样的方法来做到这一点? I understand that some questions have already been done about it, but after a long research I have not got to a conclusion.我知道关于它已经做了一些问题,但经过长时间的研究我还没有得出结论。

During my research I noticed that a UISearchBar on the NavigationBar ( to build the Scope Bar ) is only possible for UITableViewControllers , Am I right?在我的研究过程中,我注意到 NavigationBar 上的UISearchBar (用于构建 Scope Bar )仅适用于UITableViewControllers ,我说得对吗? So I think that can not be an approach.所以我认为这不是一种方法。

My next idea was to use a UISegmentedControl placed manually below the NavigationBar and then use the Containment Api to change to the different ViewControllers for this Screen.我的下一个想法是使用手动放置在 NavigationBar 下方的UISegmentedControl ,然后使用 Containment Api 更改此屏幕的不同 ViewController。 The problem here, is I will have to duplicate the UISegmentedControl on all children ViewControllers.这里的问题是我必须在所有子视图UISegmentedControl上复制UISegmentedControl Is there any way to not have to duplicate that?有没有办法不必复制它?

Another approach I tried was doing my own titleView for the NavigationBar with a NavigationBar and a UISegmentedControl below.我尝试的另一种方法是使用 NavigationBar 和下面的UISegmentedControl为 NavigationBar 做我自己的 titleView。 I don't like this idea, neither it went well trying to replicate the NavigationBar.我不喜欢这个想法,尝试复制 NavigationBar 也不顺利。

Finally, another approach I thought was using a UIPageViewController .最后,我认为另一种方法是使用UIPageViewController Although this approach sounds a good idea to me, I think I will also have to duplicate the UISegmentedControl .虽然这种方法对我来说听起来不错,但我认为我还必须复制UISegmentedControl

In the end I think the best solution is to have a UISegmentControl on the NavigationBar, but I am not seeing how to implement this.最后,我认为最好的解决方案是在 NavigationBar 上有一个UISegmentControl ,但我没有看到如何实现这一点。

What do you think is the best approach to accomplish my ideia?您认为实现我的想法的最佳方法是什么? I thought that it would be easy because it is a pattern I see in many apps.我认为这很容易,因为这是我在许多应用程序中看到的模式。 Any suggestions?有什么建议?

I am doing this on XCode 6.1.1 using Swift for iOS 8.我在 XCode 6.1.1 上使用 Swift for iOS 8 执行此操作。

Thanks a lot for your help.非常感谢你的帮助。

You can get this effect by adding the segment as the title view and setting your desired prompt.您可以通过将段添加为标题视图并设置所需的提示来获得此效果。 In interface builder it looks like this:在界面生成器中,它看起来像这样:在此处输入图片说明

Add a UIToolbar to your view.UIToolbar添加到您的视图中。 Doesn't matter if you add it through code or interface builder.如果您通过代码或界面构建器添加它并不重要。 Then you add your UISegmentedControl as custom view of an UIBarButtonItem然后将UISegmentedControl添加为UIBarButtonItem自定义视图

let toolbar = UIToolbar()
toolbar.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(toolbar)
NSLayoutConstraint.activate([
    toolbar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
    toolbar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
    toolbar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
])

// Add SegmentedControl like this:
toolbar.setItems([UIBarButtonItem(customView: mySegmentedControl)], animated: false)
toolbar.delegate = self

Then implement this delegate method (See documentation for UIBarPosition.topAttached )然后实现这个委托方法(见UIBarPosition.topAttached 文档

extension MyViewController: UIToolbarDelegate {
    public func position(for bar: UIBarPositioning) -> UIBarPosition {
        .topAttached
    }
}

Then you have what you need.然后你就有了你需要的东西。 But there's still a separator line between the navigation bar and the toolbar.但是导航栏和工具栏之间仍然有一条分隔线。 To get rid of it use this extension methods and call them in viewWillAppear and viewWillDisappear :要摆脱它,请使用此扩展方法并在viewWillAppearviewWillDisappear调用它们:

extension UINavigationBar {
    func hideHairline() {
        // Hide border line of navigation bar since we're showing a toolbar
        if #available(iOS 13.0, *) {
            standardAppearance.shadowColor = nil
            standardAppearance.shadowImage = nil
        } else {
            shadowImage = UIImage()
            setBackgroundImage(UIImage(), for: .default)
        }
    }
    
    func restoreHairline() {
        // Hide border line of navigation bar since we're showing a toolbar
        if #available(iOS 13.0, *) {
            standardAppearance.shadowColor = .separator
        } else {
            shadowImage = nil
            setBackgroundImage(nil, for: .default)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM