使用自动布局使UIView和内容展开以填充UIToolbar中的可用水平空间 [英] Make UIView and contents expand to fill available horizontal space in UIToolbar with Auto Layout
问题描述
我有一个UIBarButtonItem
和UIView
,其中包含两个UITextField
控件,都在UIToolbar
内.
I have a UIBarButtonItem
and a UIView
containing two UITextField
controls, all within a UIToolbar
.
这是我的故事板的屏幕截图: http://i.8px.co/x8eC.png .
This is a screenshot of my storyboard: http://i.8px.co/x8eC.png.
我想创建类似以下内容: http://i.8px.co/1hvY .png .
I'd like to create something similar to this: http://i.8px.co/1hvY.png.
我正在尝试使UIView
及其子级展开,以使用自动版式填充UIToolbar
中的可用水平空间.我不知道如何完成我想要的.
I'm trying to make the UIView
and its children expand to fill the available horizontal space in the UIToolbar
with Auto Layout. I don't know how to accomplish what I want.
我为UIView实验了一个肮脏的解决方案,并能够使它的尺寸正确一些.
I experimented with a dirty solution for the UIView and was able to have it size somewhat correctly.
view.width = toolBar.frame.width - button.width
// where view is the UIView, toolBar is the UIToolBar, and button is the UIBarButtonItem
我认为这不是很优雅.这是通过编程方式完成的,UIView
不符合标准的UIToolbar
边距,并且解决方案不适用于UITextFields
.
I don't believe this is very elegant. This is done programmatically, the UIView
doesn't conform to the standard UIToolbar
margins, and the solution doesn't apply to the UITextFields
.
推荐答案
恐怕您必须以编程方式执行此操作.
I'm afraid you'd have to do this programmatically.
尝试一下:
self.toolBarview.frame = (CGRect) {.origin = self.toolBarview.frame.origin.x, self.toolBarview.frame.origin.y, .size = [UIScreen mainScreen].bounds.size.width - self.barButton.width - 40, self.toolBarview.frame.size.height};
此处的工具Barview是 UIView ,其中包含2个 UITextField . buffer 值为40,您需要减去该值以进行填充.
Here toolBarview is the UIView which has the 2 UITextField's. 40 is the buffer value that you need to subtract to give the padding.
/*编辑*/
您应向UITextField
提供适当的约束,如下所示:
You should give the appropriate constraints to the UITextField
as shown below:
这篇关于使用自动布局使UIView和内容展开以填充UIToolbar中的可用水平空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!