调整的UITextField的宽度景观填写栏 [英] Adjust width of UITextField to fill toolbar in landscape

查看:342
本文介绍了调整的UITextField的宽度景观填写栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIToolbar ,我添加了一个的UITextField 酒吧中间的(这被添加为的UIBarButtonItem )与另一个的UIBarButtonItem (操作按钮)旁边。我在最左边和最右边加灵活的空间栏按钮项。它的肖像看起来不错,但是当我旋转为横向,它集中他们看起来不错,但我需要的文本字段中得到延伸,以填补宽度,推动最后一个按钮清晰的权利 - 酷似它在消息 - 上iPhone和iPad。

In a UIToolbar, I have added a UITextField to the middle of the bar (which gets added as a UIBarButtonItem) with another UIBarButtonItem (Action button) next to it. I added flexible space bar button items at the very left and very right. It looks great on portrait, but when I rotate to landscape it centers them and looks ok, but I need the text field to get stretched to fill the width and push the last button clear to the right - exactly like it does in Messages - on iPhone and iPad.

我想自动布局会​​做的伎俩,但苹果指出,你不能创建的UIBarButtonItem 取值约束。事实上,当我选择任何一个栏按钮或文本字段它不会允许创建任何限制。

I thought Auto Layout would do the trick but Apple states you cannot create constraints for UIBarButtonItems. And indeed when I select any of the bar buttons or the text field it will not allow creating any constraints.

能否请你让我知道如何做到这一点?谢谢!

Could you please let me know how to accomplish this? Thanks!

编辑:我试着固定/灵活的空间一些其他的组合。当我不添加任何的文本字段,并分享按钮都推到左边:结果

I've tried a few other combinations with fixed/flexible spaces. When I don't add any, the text field and share button are pushed over to the left:

如果不是我定的空间,它看起来完全一样的如上0宽度,或者如果我设置的宽度,然后它显然会推他们。在这最后的空间极右没有影响 - 所以它熄灭屏幕它不是固定的,防止最右侧

If instead I do fixed spaces, it looks the exact same as above with 0 widths, or if I set a width then it obviously will push them over. That last space on the far right has no effect - it's not fixed against the far right side so it goes off the screen.

我试图在左侧和柔性在右侧,与中间的一个固定的(或没有中间一个)固定的空间,它看起来像上面的截图。然后,我改变了中间的一个灵活的,它原来是这样的:结果

I tried a fixed space on the left and a flexible on the right, with the middle one fixed (or no middle one), and it looks like the screenshot above. I then changed the middle one to flexible and it turned out like this:

如果我更改了第一个灵活的,没有中间(或者,如果我加一个固定的),并flxed在右边,它移动到正确的:结果

If I change the first to flexible, none in the middle (or if I add a fixed one), and flxed on the right, it is moved over to the right:

如果第一个是灵活的,中间是灵活的,右边是固定的,这是它的外观:结果

If the first is flexible, middle is flexible, and right is fixed, this is how it looks:

在所有情况下,文本字段的宽度保持,因为它最初是建立在故事板相同的静态值。我认为问题是,当你通过拖动边框设置宽度,没有蓝色的指导方针,以捕捉到,因此将始终保持相同的宽度。

In all cases the width of the text field remains the same static value as it was originally set up in the storyboard. I think the problem is that when you set the width by dragging the frame, there are no blue guidelines to snap to so it will always remain that same width.

我错过的空间任意组合?如果我不能实现在界面生成器所需的行为,怎么会之一,code做到这一点?

Did I miss any combination of spaces? If I cannot implement the desired behavior in the interface builder, how would one accomplish this in code?

推荐答案

要获得一个水平伸展文本字段,我不得不手动设置它的 viewWillAppear中,也当方向改变 - 我用 viewWillTransitionToSize:withTransitionCoordinator:

To obtain a horizontally stretching text field, I had to manually set its frame in viewWillAppear and also when the orientation changes - I used viewWillTransitionToSize:withTransitionCoordinator::

[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  __nonnull context) {
    self.textField.frame = CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width - self.actionButton.width - 55, self.inputTextField.frame.size.height);
} completion:^(id<UIViewControllerTransitionCoordinatorContext>  __nonnull context) {
}];

结果
previous溶液(不再是最新的操作系统对我的作品):

viewDidLoad中补充一点:

self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;

这篇关于调整的UITextField的宽度景观填写栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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