导航栏和searchBar的使用和tableview
import UIKit
class HomeViewController: BaseViewController ,UITableViewDelegate,UITableViewDataSource{
override func viewDidLoad() {
super.viewDidLoad()
let titleViewBG = UIView(frame:CGRect(x:10,y:10,width:312,height:30))
let searchBar = UISearchBar(frame:CGRect(x:0,y:0,width:312,height:30))
searchBar.placeholder = "请输入搜索内容"
searchBar.tintColor = UIColor.init(red: 1, green: 0.11, blue: 0.5, alpha: 1)
searchBar.backgroundColor = UIColor.init(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)
searchBar.setSearchFieldBackgroundImage(setImageColor(UIColor.white, searchBar.bounds.size), for: UIControl.State.normal)
searchBar.backgroundImage = setImageColor(UIColor.white, searchBar.bounds.size);
titleViewBG.addSubview(searchBar)
self.navigationItem.titleView = titleViewBG
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "nav_classify_selected")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(imageBarButtonItemMethod))
let tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height), style: .plain)
tableView.rowHeight = 40
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = "哈哈哈哈哈👌"
return cell!
}
func setImageColor(_ color: UIColor, _ size:CGSize) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
@objc func imageBarButtonItemMethod() {
print("ssdsdd")
}
}