ios - 使用自动布局使 UIView 和内容扩展以填充 UIToolbar 中的可用水平空间

标签 ios swift autolayout constraints

我有一个 UIBarButtonItem和一个 UIView包含两个 UITextField控件,都在 UIToolbar 中.

这是我的 Storyboard的截图:http://i.8px.co/x8eC.png .

我想创建类似这样的东西:http://i.8px.co/1hvY.png .

我正在尝试制作 UIView及其子项扩展以填充 UIToolbar 中的可用水平空间使用自动布局。我不知道如何完成我想要的。

我为 UIView 尝试了一个肮脏的解决方案,并且能够让它的大小稍微正确一些。

view.width = toolBar.frame.width - button.width 
// where view is the UIView, toolBar is the UIToolBar, and button is the UIBarButtonItem

我不认为这很优雅。这是以编程方式完成的,UIView不符合标准 UIToolbar边距,解决方案不适用于 UITextFields .

最佳答案

恐怕您必须以编程方式执行此操作。

试试这个:

  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}; 

这里的 toolBarview 是 UIView,它有 2 个 UITextField。 40 是您需要减去以提供填充的缓冲区值。

/* 编辑 */

您应该为 UITextField 提供适当的约束,如下所示:

Textfield Constraints

关于ios - 使用自动布局使 UIView 和内容扩展以填充 UIToolbar 中的可用水平空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32131432/

相关文章:

ios - 当键盘出现 Swift 时出现移动 View 问题

ios - 为什么 SKShapeNode 会撕裂?

swift - 使用协议(protocol)类型属性对结构进行解码/编码

swift - 如何以 littleEndian 方式在 Swift 中对 NSData 输出进行字节反转?

swift - => 运算符在 Swift 中意味着什么?

ios - 更改 UIAlertView 中按钮的辅助功能标签 - iOS

iphone - iPhone上的来电后,Phonegap HTML5音频无法继续播放

ios - 无法在 Xcode 11 的 Storyboard 中设置 UIView 的比例高度

objective-c - NSInvalidUnarchiveOperationException : Could not instantiate class named NSLayoutConstraint

ios - 是否可以仅使用 interfacebuilder 在自动布局中以相同宽度并排对齐 3 个 View ?