以编程方式定位UIToolbar的最佳方式(使用或不使用UIToolbarDelegate)?

问题描述 投票:1回答:1

screen shot with UISegmentedControl below the navigation bar

我正在Playgound中实现导航栏下方的分段控件。

这似乎是一个经典问题,有人问过:

UIBarPositioningDelegate的文件中,它说,

UINavigationBarDelegate,UISearchBarDelegate和UIToolbarDelegate协议扩展了此协议,以允许在屏幕上定位这些条。

UIBarPosition的文档中:

案例顶部

指定该栏位于其包含视图的顶部。

UIToolbar.delegate的文档中:

当工具栏由导航控制器管理时,您可能无法设置委托。默认值为nil。

我目前的解决方案如下(注释掉的代码是为了参考和方便):

import UIKit
import PlaygroundSupport

class ViewController : UIViewController, UIToolbarDelegate
{
    let toolbar : UIToolbar = {
        let ret = UIToolbar()

        let segmented = UISegmentedControl(items: ["Good", "Bad"])

        let barItem = UIBarButtonItem(customView: segmented)

        ret.setItems([barItem], animated: false)
        return ret
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(toolbar)
        // toolbar.delegate = self
    }

    override func viewDidLayoutSubviews() {
        toolbar.frame = CGRect(
            x: 0,
            y: navigationController?.navigationBar.frame.height ?? 0,
            width: navigationController?.navigationBar.frame.width ?? 0,
            height: 44
        )
    }

    func position(for bar: UIBarPositioning) -> UIBarPosition {
        return .topAttached
    }
}


//class Toolbar : UIToolbar {
//    override var barPosition: UIBarPosition {
//        return .topAttached
//    }
//}

let vc = ViewController()
vc.title = "Try"
vc.view.backgroundColor = .red

// Another way to add toolbar...
// let segmented = UISegmentedControl(items: ["Good", "Bad"])
// let barItem = UIBarButtonItem(customView: segmented)
// vc.toolbarItems = [barItem]

// Navigation Controller
let navVC = UINavigationController(navigationBarClass: UINavigationBar.self, toolbarClass: UIToolbar.self)

navVC.pushViewController(vc, animated: true)
navVC.preferredContentSize = CGSize(width: 375, height: 640)
// navVC.isToolbarHidden = false


// Page setup
PlaygroundPage.current.liveView = navVC
PlaygroundPage.current.needsIndefiniteExecution = true

如你所见,这不使用UIToolbarDelegate

在这种情况下,UIToolbarDelegate(提供position(for:))如何发挥作用?既然我们可以随时定位(手动或使用自动布局),那么UIToolbarDelegate的用例是什么?

@Leo Natan在the first question link above的回答提到了UIToolbarDelegate,但似乎工具栏放在Interface Builder中。

此外,如果我们不在这里使用UIToolbarDelegate,为什么我们不使用普通的UIView而不是UIToolbar

ios swift cocoa-touch uitoolbar uitoolbarposition
1个回答
1
投票

试试这个

UIView *containerVw = [[UIView alloc] initWithFrame:CGRectMake(0, 64, 320, 60)];
containerVw.backgroundColor = UIColorFromRGB(0xffffff);
[self.view addSubview:containerVw];

UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 124, 320, 1)];
bottomView.backgroundColor = [UIColor grayColor];
[self.view addSubview:bottomView];

UISegmentedControl *sg = [[UISegmentedControl alloc] initWithItems:@[@"Good", @"Bad"]];
sg.frame = CGRectMake(10, 10, 300, 40);
[view addSubview:sg];

for (UIView *view in self.navigationController.navigationBar.subviews) {
    for (UIView *subView in view.subviews) {
        [subView isKindOfClass:[UIImageView class]];
        subView.hidden = YES;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.