class IDSearchBar: UISearchBar {
}
var contentInset: UIEdgeInsets? {
didSet {
self.layoutSubviews()
}
}
override func layoutSubviews() {
super.layoutSubviews()
// view是searchBar中的唯一的直接子控件
for view in self.subviews {
// UISearchBarBackground与UISearchBarTextField是searchBar的简介子控件
for subview in view.subviews {
// 找到UISearchBarTextField
if subview.isKindOfClass(UITextField.classForCoder()) {
if let textFieldContentInset = contentInset { // 若contentInset被赋值
// 根据contentInset改变UISearchBarTextField的布局
subview.frame = CGRect(x: textFieldContentInset.left, y: textFieldContentInset.top, width: self.bounds.width - textFieldContentInset.left - textFieldContentInset.right, height: self.bounds.height - textFieldContentInset.top - textFieldContentInset.bottom)
} else { // 若contentSet未被赋值
// 设置UISearchBar中UISearchBarTextField的默认边距
let top: CGFloat = (self.bounds.height - 28.0) / 2.0
let bottom: CGFloat = top
let left: CGFloat = 8.0
let right: CGFloat = left
contentInset = UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)
}
}
}
}
}
searchBar.frame = CGRect(x: 80, y: 100, width: 200, height: 40)
searchBar.frame = CGRect(x: 80, y: 100, width: 200, height: 40)
// 设置contentInset searchBar.contentInset = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有