欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS中Swift UISearchController仿微信搜索框

程序员文章站 2023-12-20 08:07:22
创建一个uisearchcontroller 如果传入的searchresultscontroller为nil,则表示搜索的结果在当前控制器中显示,现在我让它在searc...

创建一个uisearchcontroller

如果传入的searchresultscontroller为nil,则表示搜索的结果在当前控制器中显示,现在我让它在searchresultvc中显示

// 创建searchresultvc
let searchresultvc = uiviewcontroller()
// 设置背景颜色为红色
searchresultvc.view.backgroundcolor = uicolor.red
let searchcontroller = uisearchcontroller(searchresultscontroller: searchresultvc)
// 设置背景颜色
searchcontroller.view.backgroundcolor = uicolor (red: 0.97, green: 0.97, blue: 0.97, alpha: 1.0)
// 默认为yes,设置开始搜索时背景显示与否
// searchcontroller.dimsbackgroundduringpresentation = false
// 默认为yes,控制搜索时,是否隐藏导航栏
// searchcontroller.hidesnavigationbarduringpresentation = false
// 将搜索框视图设置为tableview的tableheaderview
tableview.tableheaderview = searchcontroller.searchbar

iOS中Swift UISearchController仿微信搜索框

添加searchbar

设置搜索框

// 搜索框
let bar = searchcontroller.searchbar
// 样式
bar.barstyle = .default
// 设置光标及取消按钮的颜色
bar.tintcolor = rgba(r: 0.12, g: 0.74, b: 0.13, a: 1.00)
// 设置代理
bar.delegate = self

iOS中Swift UISearchController仿微信搜索框

设置光标及取消按钮的颜色

去除背景

// 去除背景及上下两条横线
bar.setbackgroundimage(uiimage(), for: .any, barmetrics: .default)

iOS中Swift UISearchController仿微信搜索框

去除背景及上下两条横线

添加右侧语音按钮

// 右侧语音
bar.showsbookmarkbutton = true
bar.setimage(#imageliteral(resourcename: "voicesearchstartbtn"), for: .bookmark, state: .normal)
监听语音按钮的点击
// mark:- uisearchbardelegate
extension lxfcontactviewcontroller: uisearchbardelegate {
  func searchbarbookmarkbuttonclicked(_ searchbar: uisearchbar) {
    lxflog("点击了语音按钮")
  }
}

iOS中Swift UISearchController仿微信搜索框

右侧语音效果

iOS中Swift UISearchController仿微信搜索框 

以上所述是小编给大家介绍的ios中swift uisearchcontroller仿微信搜索框,希望对大家有所帮助

上一篇:

下一篇: