使用自动布局使UIView和内容展开以填充UIToolbar中的可用水平空间 [英] Make UIView and contents expand to fill available horizontal space in UIToolbar with Auto Layout

查看:58
本文介绍了使用自动布局使UIView和内容展开以填充UIToolbar中的可用水平空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIBarButtonItemUIView,其中包含两个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆