【问题标题】:Add SearchBar and Scope Programmatically (iOS) [closed]以编程方式添加 SearchBar 和范围(iOS)[关闭]
【发布时间】:2021-04-10 18:06:14
【问题描述】:

我试图弄清楚如何以编程方式将搜索栏和范围作为标题添加到我的 UIViewController 我希望它位于安全区域的顶部。问题是我不知道将其添加到 UIViewController 和栏按钮项标题下方安全区域顶部的代码。

func customizeSearch() {
    let searchBar = UISearchBar(frame: CGRect(x:0, y:0, width: (UIScreen.main.bounds.width), height: 70))
    searchBar.backgroundColor = UIColor.black
    searchBar.delegate = self
    searchBar.scopeButtonTitles = ["All", "Forwards", "Defensemen", "Goalies"]
    /*self.tableView.tableHeaderView = searchBar if I had a tableview, I have a vc instead.*/
  }

【问题讨论】:

    标签: swift xcode uiviewcontroller


    【解决方案1】:

    您可能希望将搜索栏添加到视图控制器的导航栏(如 VC 的标题)。如果是这种情况,您可以将其分配给导航项的titleView

    self.navigationItem.titleView = searchBar
    

    如果您想了解更多详情,请查看此链接:Search bar in navigation of vc

    【讨论】:

    • 好吧,搜索栏出现了,但我想知道如何将它放在安全区域顶部的标题下方,我应该指定更多。此外,我的 scopedbar 似乎也没有添加。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多