How do I create a keyboard accessory view with Auto Layout in Interface Builder?

老子叫甜甜 提交于 2019-12-04 03:49:12

问题


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.

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?


回答1:


Would you mind doing it programmatically?

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

EDIT: 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.



来源:https://stackoverflow.com/questions/27573045/how-do-i-create-a-keyboard-accessory-view-with-auto-layout-in-interface-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!