给我一个关于 UIReturnKey 颜色/禁用属性的建议

发布于 2024-12-02 02:33:19 字数 1988 浏览 1 评论 0原文

在某些情况下,我的应用程序看起来像下面的快照。

在此处输入图像描述

有一个 UITextField 用于从用户处获取部门名称。最初,导航栏中的“完成”按钮设置为禁用。即,将右栏按钮的 enabled 属性设置为 NO

文本字段的 UIKeyBoard 是由以下代码制作的

fieldForDepartment.keyboardAppearance = UIKeyboardAppearanceAlert; //This provides the black appearance to the keyboard
fieldForDepartment.returnKeyType = UIReturnKeyDone; //This set the return key type as Done
fieldForDepartment.clearButtonMode = UITextFieldViewModeWhileEditing; //This is used because i want a small cross button in the right end of my text field to clear the text field value

(这里 fieldForDepartment 是 UITextField实例的名称)

我想启用导航栏中的“完成”按钮 if &仅当文本字段中至少应有一个字母(甚至可能是一个空格)。如果我的文本字段中没有字母,“完成”按钮应该被禁用。

为此,我实现了这段代码...

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([[fieldForDepartment.text stringByReplacingCharactersInRange:range withString:string] isEqual:@""] || [fieldForDepartment.text stringByReplacingCharactersInRange:range withString:string] == nil) {
        rightBarButton.enabled = NO;
    }
    else {
        rightBarButton.enabled = YES;
    }
    return YES;
}

这个方法 textField: shouldChangeCharactersInRange: 在键盘上按下每个字母时都会被调用。

[fieldForDepartment.text stringByReplacingCharactersInRange:range withString:string] 将从 UITextField 中获取当前字符串(包括最后按下的字母),

因此,当在文本字段中按下字母时,应用程序将如下所示。 .

在此处输入图像描述

我的问题来了..

UIKeyboard 中的“完成”按钮始终是突出显示。因此这违反了导航栏中“完成”按钮的行为。我想以与导航栏的“完成”按钮相同的方式禁用/启用 UIKeyboard 的“完成”按钮。是否可以?

我脑海中的一些问题是..

我可以禁用 UIKeyboard 的“完成”按钮吗?

或者

我可以改变背景颜色吗? UIKeyboard 中“完成”按钮的选择颜色? (这样用户可能会感觉按钮无法按下)

或者

我可以隐藏 UIKeyboard 中的“完成”按钮吗?

请建议哪一种是解决我的问题的好方法...

提前致谢

My application is looking like the below snap in some scenario.

enter image description here

There is one UITextField to get the department name from the user. Initially the "Done" button in navigation bar is set to disabled. That is, set right bar button's enabled property to NO

The UIKeyBoard of the text field was made by the below code

fieldForDepartment.keyboardAppearance = UIKeyboardAppearanceAlert; //This provides the black appearance to the keyboard
fieldForDepartment.returnKeyType = UIReturnKeyDone; //This set the return key type as Done
fieldForDepartment.clearButtonMode = UITextFieldViewModeWhileEditing; //This is used because i want a small cross button in the right end of my text field to clear the text field value

(Here fieldForDepartment is the UITextField instance's name)

I want to enable the "Done" button in the navigation bar if & only if, there should be atleast one letter in the text field (even may be a space). The "Done" button should get disable if there is a no letter in my text field.

For that, I implemented this code...

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([[fieldForDepartment.text stringByReplacingCharactersInRange:range withString:string] isEqual:@""] || [fieldForDepartment.text stringByReplacingCharactersInRange:range withString:string] == nil) {
        rightBarButton.enabled = NO;
    }
    else {
        rightBarButton.enabled = YES;
    }
    return YES;
}

This method textField: shouldChangeCharactersInRange: get called for each letter press in the keyboard.

[fieldForDepartment.text stringByReplacingCharactersInRange:range withString:string] will bring the current string(included the last letter pressed) from the UITextField

So when pressing a letter in the text field the application will be like this...

enter image description here

Here my problem comes..

The "Done" button in the UIKeyboard is always highlighted. So this violates the behavior of the "Done" button in the navigation bar. I want to disable/enable the UIKeyboard's Done button in the same way the navigation bar's Done button works. Is it possible?

Some of the questions in my mind are..

Can i disable the "Done" button of UIKeyboard?

or

Can i just change the background color & selection color of the Done button in UIKeyboard? (So that the user may feel that the button can not be pressed)

or

Can i hide the Done button in UIKeyboard?

Please suggest which one is the good way to solve my problem...

Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找我们的幸福 2024-12-09 02:33:19

界面构建器属性的文本特征部分中有一个自动启用返回键设置。它完全满足您的需要,只需确保检查一下即可。

如果您需要在代码中进行更改,请使用 UITextField 的 enablesReturnKeyAutomatically 属性(这实际上是在该类实现的 UITextInputTraits 协议中定义的)

There is an Auto-enable Return Key setting in the Text trait's section of the Interface builder properties. It does exactly what you need, just make sure to check it.

If you need to do the change in the code, use enablesReturnKeyAutomatically property of the UITextField (this is actually defined in the UITextInputTraits protocol that the class implements)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文