我正在测试我的 UI,我发现搜索栏有点太窄了,我不喜欢.我还想确保视力较差或手动灵活性较差的人在调出他们想要的界面时不会遇到问题.
I'm testing out my UI and I find the search bar a little bit too narrow for my liking. I also want to make sure people with poorer vision or poorer manual dexterity have no issues bringing up the interface they want.
所以,我想做的是调整 UISearchbar
内 UITextfield
的高度.
So, what I would like to do is Adjust the height of the UITextfield
inside UISearchbar
.
我尝试过的:1、在Storyboard中,添加UISearchbar
高度约束——结果:搜索栏大小增加,但里面的UITextField
保持不变.
What I have tried:
1. In Storyboard, add UISearchbar
Height constraint - Results: the searchbar size increases, but the UITextField
inside stays the same.
UISearchbar
内的 UITextField
并修改其高度 - 结果:控制台输出显示参数已修改,但在屏幕上,UITextField
高度保持不变.UITextField
inside UISearchbar
and modify its height - Results: The console output shows that the parameter is modified, but on screen, the UITextField
height remains the same.注意 - 我可以使用方法 2 修改 UITextField
的其他参数,并且更改会反映在屏幕上,所以我知道我正在访问 UITextField
Note - I can modify other parameters of the UITextField
using method 2 and the change is reflected on screen so I know I am getting to the UITextField
方法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 中一样,当用户设置设置时,它会在其自动布局中使用这些参数并在可以时抱怨't 而不是在没有反馈的情况下忽略设置.
I think it has something to do with autolayout somehow being able to disregard parameters regardless of where it is set. I would like to continue to use autolayout since it does a lot of work for me, but I wish it would behave like it does in Storyboard where when a user sets a setting it will use those parameters in its autolayout and complain when it can't as opposed to just ignoring the setting without feedback.
回应马赫什.我不太了解客观的 C++,但我尽力将您写的内容快速转换为 swift.这就是我所拥有的:
In response to Mahesh. I'm don't know much objective C++ but I tried my best to convert what you wrote into swift. This is what I have:
(在我的 viewcontroller.swift 中)
(Inside my 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 混合,所以我也必须改变它.
I've kept my code for getting to the textfield since I had some issues converting your code to swift. For the CGRectMake - swift complained about various types including that topPadding wasn't a CGFloat and for the last variable (Y size), again it did not like mixing CGFloat with Float so I had to change that too.
不幸的是,它似乎不起作用.我在 Storyboard 中将 UISearchbar 的高度更改为 80,然后我得到了一个非常高的搜索栏,其文本字段覆盖了总高度的 1/3.
Unfortunately, it does not appear to work. I changed the UISearchbar height to 80 in storyboard and I just got a very tall searchbar with the textfield covering about 1/3 of the total height.
编辑 #2:合并 Yuyutsu 和更正版的 Mahesh 代码.仍然不完美,但更接近.Yuyutsu 的代码有效,但正如我在评论中提到的那样,该字段不居中,第一次加载视图时调整大小也是可见的(从高度 A 跳到高度 B).另一个缺陷是,因为一旦方向改变,调整大小是在 viewDidAppear
完成的,所以字段会返回到固有大小.
Edit #2: Incorporating Yuyutsu and corrected version of Mahesh code.
Still not perfect, but closer. Yuyutsu's code works but as mentioned in my comments, the field does not center, the resize is also visible (jump from height A to height B) when the view is first loaded. One additional deficiency is that because the resize is done at viewDidAppear
once the orientation changes, the field returns to the intrinsic size.
我的代码基于 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 的代码,我想知道我还能在哪里进行此调整,结果发现 link 通过另一个 stackoverflow 帖子.根据我阅读的内容,我看到 Mahesh 的答案应该有效.这就是我意识到它为什么不起作用的地方 - 我需要覆盖 func viewWillLayoutSubviews()
.
Looking at Yuyutsu's code I wanted to know where else I could put this adjustment and I came across link via another stackoverflow post. Based on what I read, I saw that Mahesh's answer should work. This is where I realized why it wasn't working - I needed to override 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)
}
}
}
}
所以现在,唯一剩下的就是尝试使文本字段在视图可见之前设置为大小,这样用户就不会看到大小变化.
So right now, the only thing left is to try and make it so the textfield is the set size prior to the view being visible so there is no size shift that is visible to the user.
最终完全工作的代码在 Yuyutsu 的额外帮助下,下面的代码可以完成我想要的一切 - 从设定的大小开始,字段居中,可以很好地处理旋转.
Final Fully working code With Yuyutsu's additional help, code below does everything I want - starts off at the set size, field is centered, deals with rotation fine.
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
}
}
}
}
谢谢悠悠.感谢 Mahesh 从一开始就提出了最终解决方案,只是遗漏了一些关键部分.
Thanks Yuyutsu. Thanks Mahesh for pretty much coming up with the final solution right from the start, just missing a few critical bits.
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
for subView in searchBars.subviews {
for subsubView in subView.subviews {
if let textField = subsubView as? UITextField {
var bounds: CGRect
bounds = textField.frame
bounds.size.height = 35 //(set height whatever you want)
textField.bounds = bounds
textField.borderStyle = UITextBorderStyle.RoundedRect
// textField.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
textField.backgroundColor = UIColor.redColor()
// textField.font = UIFont.systemFontOfSize(20)
}
}
}
}
这可能对你有帮助:)
这篇关于UISearchbar TextField 的高度可以修改吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!