iOS UITextField InputAccessoryView & InputView的使用方法

转载地址:http://lqcjdx.blog.163.com/blog/static/20748924120138209730559/

参考资料:yandai2008279的博客


UITextFields and UITextViews havean inputAccessoryView property,which you can set to any view, that is automatically displayedabove and animated with the keyboard.

Note that the view you use shouldneither be in the view hierarchy elsewhere, nor should you add itto some superview, this is done for you.

翻译:
UITextFields和UITextView有一个inputAccessoryView的属性,当你想在键盘上展示一个自定义的view时,你就可以设置该属性。你设置的view就会自动和键盘keyboard一起显示了。

需要注意的是,你所自定义的view既不应该处在其他的视图层里,也不应该成为其他视图的子视图。其实也就是说,你所自定义的view只需要赋给属性inputAccessoryView就可以了,不要再做其他多余的操作。

我们在使用UITextView和UITextField的时候,可以通过它们的inputAccessoryView属性给输入时呼出的键盘加一个附属视图,通常是UIToolBar,用于回收键盘。

代码如下:

//定义一个toolBar UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)]; //设置style [topView setBarStyle:UIBarStyleBlack]; //定义两个flexibleSpace的button,放在toolBar上,这样完成按钮就会在最右边 UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil]; UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil]; //定义完成按钮 UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard)]; //在toolBar上加上这些按钮 NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil]; [topView setItems:buttonsArray]; // [textView setInputView:topView]; [textView setInputAccessoryView:topView]; //隐藏键盘 -(void)resignKeyboard { [textView resignFirstResponder]; }

注意:

       inputView就是显示键盘的view,如果重写这个view则不再弹出键盘,而是弹出自己的view.如果想实现当某一控件变为第一响应者时不弹出键盘而是弹出我们自定义的界面,那么我们就可以通过修改这个inputView来实现,比如弹出一个日期拾取器。

        inputView不会随着键盘出现而出现,设置了InputView只会当UITextField或者UITextView变为第一相应者时显示出来,不会显示键盘了。设置了InputAccessoryView,它会随着键盘一起出现并且会显示在键盘的顶端。InutAccessoryView默认为nil.

苹果说明文档:

// Presented when object becomes first responder.  If set to nil, reverts to following responder chain.  If
// set while first responder, will not take effect until reloadInputViews is called.
@property (readwrite, retain) UIView *inputView;            
@property (readwrite, retain) UIView *inputAccessoryView;

效果图:


InputtAccessoryView的使用 - 〇①世界 - 云端的小牛牛


深入学习:

来自:iamfreedom2011的专栏


我们在使用UITextView和UITextField的时候,可以通过它们的inputAccessoryView属性给输入时呼出的键盘加一个附属视图,通常是UIToolBar,用于回收键盘。

但是当我们要操作的视图不是UITextView或UITextField的时候,inputAccessoryView就变成了readonly的。

这时我们如果还想再加inputAccessoryView,按API中的说法,就需要新建一个该视图的子类,并重新声明inputAccessoryView属性为readwrite的。比如我们要实现点击一个tableView的一行时,呼出一个UIPickerView,并且附加一个用于回收PickerView的toolbar。因此我们自建一个UITableViewCell类,并声明inputAccessoryView和inputView为readwrite的,并且重写它们的get方法,这样在某个tableviewcell变成第一响应者时,它就会自动呼出inputView和inputAccessoryView;

复制代码
1 @interface MyTableViewCell : UITableViewCell<UIPickerViewDelegate,UIPickerViewDataSource>
2 {
3     UIToolbar *_inputAccessoryView;
4     UIPickerView *_inputView;
5 }
6 @property(strong,nonatomic,readwrite) UIToolbar *inputAccessoryView;
7 @property(strong,nonatomic,readwrite) UIPickerView *inputView;
8 @end
复制代码

.m中的get方法:

复制代码
 1 -(UIToolbar *)inputAccessoryView
 2 {
 3     if(!_inputAccessoryView)
 4     {
 5         UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
 6 //        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItem target:self action:@selector(dodo)];
 7         UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dodo)];
 8         toolBar.items = [NSArray arrayWithObject:right];
 9         return toolBar;
10     }
11     return _inputAccessoryView;
12 }
13 -(UIPickerView *)inputView
14 {
15     if(!_inputView)
16     {
17       UIPickerView *  pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 200, 320, 200)];
18         pickView.delegate =self;
19         pickView.dataSource = self;
20         pickView.showsSelectionIndicator = YES;//显示选中某行后的那个标志栏
21         return pickView;
22     }
23     return _inputView;
24 }
25 -(void)dodo
26 {
27     [self resignFirstResponder];
28 }
29 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
30 {
31     return 1;
32 }
33 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
34 {
35     return [NSString stringWithFormat:@"%d",row];
36 }
37 // returns the # of rows in each component..
38 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
39 {
40     return 5;
41 }
复制代码

但是这时运行后还是没有反应,最后在一个网页中查到这样的话:

What is the best, we aren't limited to use this feature on UITextFields only. Because of fact that UIView inherits from UIResponder, we can attach this behaviour to all views, for example to a button or a table cell. To do that we have to override canBecomeFirstRespondermethod in our subclass. For example, the UIButton subclass implementation can look like this:

    implementation CustomButton { } //it is UIButton subclass
    
    @synthesize inputView, inputAccessoryView;
    
    - (BOOL) canBecomeFirstResponder {
        return YES;
    }
    
    - (void)dealloc {
        [inputView release];
        [inputAccessoryView release];
        [super dealloc];
    }
    
    @end
因此我在.m中重写canBecomeFirstResponder方法
-(BOOL)canBecomeFirstResponder
{
    return YES;
}

但是这时运行是还是没有反应,最后我只好在代码中当cell被选中时,手动把它变成第一响应者。(难道cell被选中时不是第一响应者?)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell becomeFirstResponder];
}

运行结果:


最后附上苹果的说明文档:
inputAccessoryView
The custom accessory view to display when the object becomes the first responder. (read-only)

@property (readonly, retain) UIView *inputAccessoryView
Discussion
The default value of this property is nil. Subclasses that want to attach custom controls to either a system-supplied input view (such as the keyboard) or a custom input view (one you provide in the inputView property) should redeclare this property as readwrite and use it to manage their custom accessory view. When the receiver subsequently becomes the first responder, the responder infrastructure attaches the view to the appropriate input view before displaying it.

This property is typically used to attach an accessory view to the system-supplied keyboard that is presented for UITextField and UITextView objects.

Availability
Available in iOS 3.2 and later.
Declared In
UIResponder.h


inputView
The custom input view to display when the object becomes the first responder. (read-only)

@property (readonly, retain) UIView *inputView
Discussion
The value of this property is nil. Responder objects that require a custom view to gather input from the user should redeclare this property as readwrite and use it to manage their custom input view. When the receiver subsequently becomes the first responder, the responder infrastructure presents the specified input view automatically. Similarly, when the view resigns its first responder status, the responder infrastructure automatically dismisses the specified view.

This property is typically used to replace the system-supplied keyboard that is presented for UITextField and UITextView objects.

Availability
Available in iOS 3.2 and later.
Declared In
UIResponder.h

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 中,UITextField 的光标大小和颜色是由系统控制的,我们不能直接修改其大小,但是可以通过修改 UITextField 的 tint 属性来改变光标的颜色。 虽然不能直接修改光标大小,但我们可以通过设置 UITextField 的边框样式为 None,然后通过添加一个自定义的 UIView 作为光标来模拟实现。 以下是一个示例代码,可以在 UITextField 中添加一个自定义的光标: ```swift let customCursorView = UIView(frame: CGRect(x: 0, y: 0, width: 2, height: textField.frame.height)) customCursorView.backgroundColor = UIColor.red textField.tintColor = UIColor.clear textField.addSubview(customCursorView) ``` 在这个代码中,我们创建了一个自定义的 UIView,用于模拟光标。我们将其宽度设置为 2,高度设置为 UITextField 的高度,颜色设置为红色,并将其添加到 UITextField 上。最后,我们将 UITextField 的 tint 设置为 clear,以隐藏系统的光标。 需要注意的是,为了保证自定义光标的位置和系统光标的位置一致,我们还需要在 UITextField 的代理方法中添加以下代码: ```swift func textFieldDidChangeSelection(_ textField: UITextField) { if let selectedRange = textField.selectedTextRange { let cursorPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start) if let customCursorView = textField.subviews.last { customCursorView.frame.origin.x = textField.frame.origin.x + textField.textRect(forBounds: textField.bounds).origin.x + textField.font!.size(of: String(textField.text![..<textField.text!.index(textField.text!.startIndex, offsetBy: cursorPosition)]), constrainedToWidth: textField.frame.width).width } } } ``` 在这个代理方法中,我们获取了 UITextField 中被选中的文本范围,然后计算了光标在 UITextField 中的位置,并将自定义光标的位置进行了调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值