如何在 Interface Builder 中使用自动布局创建键盘附件视图?

How do I create a keyboard accessory view with Auto Layout in Interface Builder?(如何在 Interface Builder 中使用自动布局创建键盘附件视图?)

本文介绍了如何在 Interface Builder 中使用自动布局创建键盘附件视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将完成"按钮和分段控件添加到小数点键盘.理想情况下,我想在 Interface Builder 中使用 Auto Layout 布局键盘附件视图.

I want to add a "Done" button and a segmented control to a Decimal Pad keyboard. Ideally I would like to lay out a keyboard accessory view with Auto Layout in Interface Builder.

这甚至可能吗?我是创建一个新的 XIB 还是可以在现有的 Storyboard 场景中以某种方式进行?如何将附件视图附加到相应的文本字段?

Is this even possible? Do I create a new XIB or can I do it in the existing Storyboard scene somehow? How do I attach the accessory view to the appropriate text field?

推荐答案

您介意以编程方式进行吗?

Would you mind doing it programmatically?

通常,您将 UIToolbar 与您的项目一起带到 UITextField,但您可能需要对 UISegmentedControl 进行子视图;

Typically you take a UIToolbar to a UITextField with your items, but you may need to subview the UISegmentedControl;

UIToolbar *keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:...
// Customize segmentedControl's properties here.

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed)];

[keyboardToolbar setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, nil]];
[keyboardToolbar addSubview:segmentedControl];
[textField setInputAccessoryView:keyboardToolbar];

最初可以在 IB 中创建工具栏,但您必须将其拖出视图并拖到包含场景的左侧边栏并将其链接到参考插座,然后使用 setInputAccessoryView 分配它 在你的 viewDidLoad 中.

It is possible to create the toolbar initially in IB, but you have to drag it off the view and to the left hand sidebar containing the scene and link it to a reference outlet, then assign it with setInputAccessoryView in your viewDidLoad.

这篇关于如何在 Interface Builder 中使用自动布局创建键盘附件视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 Interface Builder 中使用自动布局创建键盘附件视图?

基础教程推荐