我正在测试我的用户界面,发现搜索栏有点太窄,想要将其高度调整一下。同时,我也希望视力较差或手部协调能力较差的人们不会在使用时遇到任何问题。
因此,我想要做的是调整UISearchbar
中的UITextField
的高度。
我尝试过以下方法:
- 在Storyboard中添加
UISearchbar
高度约束——结果:搜索栏大小增加了,但是里面的UITextField
大小没有改变。 - 访问
UISearchbar
中的UITextField
并修改其高度——结果:控制台输出显示已经修改了参数,但是屏幕上UITextField
的高度仍然保持不变。
注意:我可以使用第二种方法修改UITextField
的其他参数,并在屏幕上反映出变化,所以我知道我已经访问到了UITextField
。
Method 2的代码,在包含UISearchbar
的ViewController的viewDidLoad()中:
for tempView in (self.roomInfoSearchBar.subviews[0] as! UIView).subviews as! [UIView]
{
if let tV = tempView as? UITextField
{
var currentFrameRect = tV.frame
currentFrameRect.size.height = 80
//tV.layer.backgroundColor = UIColor.blueColor().CGColor //This works fine
//tV.layer.cornerRadius = 5 //This works fine
//tV.font = UIFont(name: "Courier", size: 40) //This works fine
println(tV.frame.height) //console output:0.0
tV.frame = currentFrameRect
println(tV.frame) //console output:(0.0, 0.0, 0.0, 80.0)
println(currentFrameRect) //console output:(0.0, 0.0, 0.0, 80.0) - redundant, just checking
println(tV.frame.height) //console output:80.0 - it says 80, but screen shows searchbar definitely not 80.
}
}
我认为这与自动布局有关,它可以在任何位置忽略参数。我想继续使用自动布局,因为它可以为我完成很多工作,但我希望它的行为类似于Storyboard中的行为,即当用户设置一个选项时,它将在其自动布局中使用这些参数,并在无法使用时提供反馈,而不是无声地忽略它。
编辑:
回应Mahesh的评论。我不太懂Objective C ++,但我尽力将您写的内容转换成Swift语言。以下是我的代码(位于viewcontroller.swift内部):func viewDIdLayoutSubviews()
{
super.viewDidLayoutSubviews()
self.roomInfoSearchBar.layoutSubviews()
var topPadding: Float = 5.0
for view in (self.roomInfoSearchBar.subviews[0] as! UIView).subviews as! [UIView]
{
if let tfView = view as? UITextField
{
var calcheight = self.roomInfoSearchBar.frame.size.height - 10
tfView.frame = CGRectMake(tfView.frame.origin.x, CGFloat(topPadding), tfView.frame.size.width, self.roomInfoSearchBar.frame.size.height - CGFloat(topPadding * 2))
}
}
}
由于我在将你的代码转换成Swift时遇到一些问题,所以我保留了获取文本字段的代码。对于CGRectMake - Swift抱怨包括topPadding不是CGFloat类型,对于最后一个变量(Y大小),它也不喜欢将CGFloat与Float混合,因此我也不得不改变它。
不幸的是,它似乎并不起作用。 我在Storyboard中将UISearchbar的高度更改为80,但只得到一个非常高的搜索栏,而文本字段仅覆盖总高度的1/3。
编辑#2:结合Yuyutsu和Mahesh代码的修正版。 仍然不完美,但更接近了。 Yuyutsu的代码有效,但如我在评论中提到的那样,该字段没有居中,当视图首次加载时,调整大小也可见(从高度A跳到高度B)。 另一个缺点是,因为调整大小是在viewDidAppear
中完成的,所以一旦方向发生变化,该字段就会返回固有大小。
基于Yuyutsu的我的代码:
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
var roomInfoSearchBarFrame = roomInfoSearchBar.frame
var newHeight: CGFloat = 60
for subView in roomInfoSearchBar.subviews
{
for subsubView in subView.subviews
{
if let textField = subsubView as? UITextField
{
var currentTextFieldFrame = textField.frame
var recenteredY = (roomInfoSearchBarFrame.height - newHeight)/2
var newTextFieldFrame = CGRectMake(textField.frame.minX, recenteredY, textField.frame.width, newHeight)
textField.frame = newTextFieldFrame
textField.borderStyle = UITextBorderStyle.RoundedRect
textField.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
//textField.backgroundColor = UIColor.redColor()
// textField.font = UIFont.systemFontOfSize(20)
}
}
}
}
查看Yuyutsu的代码后,我想知道在哪里可以放置此调整项,然后我通过另一个stackoverflow帖子发现了链接。根据我的阅读,我认为Mahesh的答案应该可以解决问题。这就是我意识到为什么它没有起作用的地方 - 我需要覆盖func viewWillLayoutSubviews()
。
当前代码:与方向更改一起保持大小。 但是大小跳跃仍然可见(不应该可见)
override func viewWillLayoutSubviews()
{
super.viewWillLayoutSubviews()
var roomInfoSearchBarFrame = roomInfoSearchBar.frame
var newHeight: CGFloat = 60
for subView in roomInfoSearchBar.subviews
{
for subsubView in subView.subviews
{
if let textField = subsubView as? UITextField
{
var currentTextFieldFrame = textField.frame
var recenteredY = (roomInfoSearchBarFrame.height - newHeight)/2
var newTextFieldFrame = CGRectMake(textField.frame.minX, recenteredY, textField.frame.width, newHeight)
textField.frame = newTextFieldFrame
textField.borderStyle = UITextBorderStyle.RoundedRect
textField.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
//textField.backgroundColor = UIColor.redColor()
// textField.font = UIFont.systemFontOfSize(20)
}
}
}
}
目前,唯一剩下的就是尝试使文本字段在视图可见之前成为设置大小,以便对用户不可见。
最终编辑:完全工作的代码在Yuyutsu的额外帮助下,下面的代码可以做到我想要的一切-从设置大小开始,字段居中,处理旋转也很好。
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
self.roomInfoSearchBar.layoutIfNeeded()
self.roomInfoSearchBar.layoutSubviews()
var roomInfoSearchBarFrame = roomInfoSearchBar.frame
var newHeight: CGFloat = 60 //desired textField Height.
for subView in roomInfoSearchBar.subviews
{
for subsubView in subView.subviews
{
if let textField = subsubView as? UITextField
{
var currentTextFieldBounds = textField.bounds
currentTextFieldBounds.size.height = newHeight
textField.bounds = currentTextFieldBounds
textField.borderStyle = UITextBorderStyle.RoundedRect
//textField.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
}
}
}
}
感谢Yuyutsu。 Mahesh感谢你基本上从一开始就提出了最终解决方案,只是缺少一些关键部分。
roomInfoSearchBar.frame
减去2 * somePadding(somePadding是您希望在searchField和textField之间留出的空间)来计算newHeight
。一旦设置了这个,您就可以在Storyboard中调整searchBar大小,textField也会相应调整。如果您想更改填充,则只需要修改viewDidLayoutSubviews()
即可。 - jansonUITextField
,并将放大镜图像添加到其leftView
中。实际上,这样做可以避免因为对UISearchBar
进行修改而导致的很多问题。 - Sulthan