ios - 以编程方式 UISearchBar 未显示

标签 ios swift uisearchbar

我以编程方式创建了一个 UISearchBar(不是 UISearchController)并将其设置为 tableview header 。但是,它没有出现。任何人都可能知道这是为什么?

课外:

var searchBar = UISearchBar()

viewDidLoad 方法中。

    searchBar.barTintColor = UIColor(red:0.54, green:0.77, blue:0.80, alpha:1.0)
    searchBar.placeholder = "Cauta cheltuieli"
    searchBar.backgroundImage = #imageLiteral(resourceName: "searchbarback")

    tableView.tableHeaderView = searchBar
    tableView.setContentOffset(CGPoint.init(x: 0, y: 44), animated: true)

最佳答案

您忘记为搜索栏指定任何尺寸,因此大小将为零。

尝试通过设置框架来设置尺寸或使用带框架的构造函数。

let searchbar = UISearchBar(frame: CGRect(x: 0.0, y: 0.0, height: 50.0, width: tableView.frame.width))

关于ios - 以编程方式 UISearchBar 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47254372/

相关文章:

Xcode 委托(delegate)链接到对象

ios - Firebase Firestore 和 Protobuf 的词法或前身问题,这两个文件夹是问题所在

swift - OS X。如何使用参数运行控制台应用程序?

ios - UINavigationBar 和 UISearchBar 之间的小差距

ios - 当键盘关闭时,如何使搜索栏中的取消按钮保持启用状态?

ios - UISearchBar : How to prevent Cancel Button from clearing text?

android - 将种子数据库与 Parse 本地数据存储集成

ios - 在 Swift 中从子字符串创建特殊的 UIButtons 文本

ios - 无法滚动ScrollView

ios - 在 SceneKit 中指向正确的方向