开发者

Give me a suggestion for UIReturnKey color/disable property

开发者 https://www.devze.com 2023-03-31 22:04 出处:网络
My application is looking like the below snap in some scenario. There is one UITextField to get the department name from the user. Initially the \"Done\" button in navigation bar is set to disabled

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

Give me a suggestion for UIReturnKey color/disable property

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

Give me a suggestion for UIReturnKey color/disable property

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 开发者_StackOverflow社区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


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消