gpt4 book ai didi

ios - 无法从自定义搜索栏修改高度 UITextField

转载 作者:行者123 更新时间:2023-11-29 01:15:45 25 4
gpt4 key购买 nike

开始我已经看了this问题并尝试实现它,但它没有改变任何东西,所以我将在这段代码中展示我自己的尝试。

就像问题所说的那样,我正在尝试更改搜索栏内 UITextField 的高度。我制作了一个自定义搜索栏以及一个自定义搜索 Controller 。

搜索栏类看起来像这样

class CustomSearchBar: UISearchBar {

var preferredFont: UIFont!
var preferredTextColor: UIColor!

override func drawRect(rect: CGRect) {
// Drawing code

// Find the index of the search field in the search bar subviews.
if let index = indexOfSearchFieldInSubviews() {
// Access the search field
let searchField: UITextField = (subviews[0]).subviews[index] as! UITextField


// Set its frame.
//searchField.frame = CGRectMake(5.0, 5.0, frame.size.width - 10.0, frame.size.height - 10.0)
searchField.frame = CGRectMake(5.0, 5.0, frame.size.width, 100)

// Set the font and text color of the search field.
searchField.font = preferredFont
searchField.textColor = preferredTextColor

// Set the background color of the search field.
searchField.backgroundColor = barTintColor
}

let startPoint = CGPointMake(0.0, frame.size.height)
let endPoint = CGPointMake(frame.size.width, frame.size.height)
let path = UIBezierPath()
path.moveToPoint(startPoint)
path.addLineToPoint(endPoint)

let shapeLayer = CAShapeLayer()
shapeLayer.path = path.CGPath
shapeLayer.strokeColor = preferredTextColor.CGColor
shapeLayer.lineWidth = 2.5

layer.addSublayer(shapeLayer)

super.drawRect(rect)
}

init(frame: CGRect, font: UIFont, textColor: UIColor){
super.init(frame: frame)

self.frame = frame
preferredFont = font
preferredTextColor = textColor

searchBarStyle = UISearchBarStyle.Prominent
translucent = false
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

func indexOfSearchFieldInSubviews() -> Int! {
var index: Int!
let searchBarView = subviews[0]

for var i=0; i<searchBarView.subviews.count; ++i {
if searchBarView.subviews[i].isKindOfClass(UITextField) {
index = i
break
}
}

return index
}

我的自定义搜索 Controller 看起来像这样

class CustomSearchController: UISearchController, UISearchBarDelegate {
var customSearchBar: CustomSearchBar!
var customDelegate: CustomSearchControllerDelegate!

init(searchResultsController: UIViewController!, searchBarFrame: CGRect, searchBarFont: UIFont, searchBarTextColor: UIColor, searchBarTintColor: UIColor){
super.init(searchResultsController: searchResultsController)

configureSearchBar(searchBarFrame, font: searchBarFont, textColor: searchBarTextColor, bgColor: searchBarTintColor)

}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?){
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

func configureSearchBar(frame: CGRect, font: UIFont, textColor:UIColor, bgColor: UIColor){
customSearchBar = CustomSearchBar(frame: frame, font: font, textColor: textColor)

customSearchBar.barTintColor = bgColor
customSearchBar.tintColor = textColor
customSearchBar.showsBookmarkButton = true
customSearchBar.setImage(UIImage(named: "recording.png"), forSearchBarIcon: UISearchBarIcon.Bookmark, state: UIControlState.Normal)
customSearchBar.setImage(UIImage(named:"record_tap.png"), forSearchBarIcon: UISearchBarIcon.Bookmark, state: UIControlState.Selected)
customSearchBar.showsCancelButton = false

customSearchBar.delegate = self

}

在我创建此搜索栏的 View Controller 中,我使用了以下代码。在我的 viewDidLoad() 中被调用

func configureCustomSearchController() {
customSearchController = CustomSearchController(searchResultsController: self, searchBarFrame: CGRectMake(0.0, 0.0, UIScreen.mainScreen().bounds.width, 100.0), searchBarFont: UIFont(name: "Futura", size: 18.0)!, searchBarTextColor: UIColor(red: 0.612, green: 0.984, blue: 0.533, alpha: 1), searchBarTintColor: UIColor.blackColor())

customSearchController.customSearchBar.placeholder = "Search.."
tblSearchResults.tableHeaderView = customSearchController.customSearchBar
customSearchController.customDelegate = self
}

我可以更改整个搜索栏的框架,请参见上面我将其设置为 100 的位置。但是我无法更改我想增大的 UITextField 的高度。我还确定我正在访问 UITextView。当我更改 drawRect 函数中的颜色时,它会发生变化。只是高度是这里的问题。有人知道我做错了什么或我需要做什么才能实现这一点。

最佳答案

我发现如果我将您的尺寸调整代码移动到 layoutSubviews() 中,我认为它会按您预期的那样工作。

override func layoutSubviews() {
// Find the index of the search field in the search bar subviews.
if let index = indexOfSearchFieldInSubviews() {
... //Added red background
}
}

override func drawRect(rect: CGRect) {...

我相信在调用 drawRect() 时,布局时间已经过去。不过,我无法真正评论这是否是最佳实现。

Reveal

关于ios - 无法从自定义搜索栏修改高度 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35213025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com