iOS7 Keyboard Return/Done/Search tint colour

旧时模样 提交于 2019-12-31 08:29:48

问题


With the new iOS7 UIView tint color it becomes pretty easy to theme an entire app quickly. It even changes the color of the text caret when editing UITextFields.

However, the keyboard's bottom right 'dismiss' button (can be Done, Search, etc) is always blue. Is there any way to change this? It would look really nice if it matched the tint color of the rest of the app.


回答1:


With a little hack maybe you can achieve the effect you are looking for. But it might not be able to pass the app review.

-(NSArray*)subviewsOfView:(UIView*)view withType:(NSString*)type{
    NSString *prefix = [NSString stringWithFormat:@"<%@",type];
    NSMutableArray *subviewArray = [NSMutableArray array];
    for (UIView *subview in view.subviews) {
        NSArray *tempArray = [self subviewsOfView:subview withType:type];
        for (UIView *view in tempArray) {
            [subviewArray addObject:view];
        }
    }
    if ([[view description]hasPrefix:prefix]) {
        [subviewArray addObject:view];
    }
    return [NSArray arrayWithArray:subviewArray];
}

-(void)addColorToUIKeyboardButton{
    for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
        for (UIView *keyboard in [keyboardWindow subviews]) {
            for (UIView *view in [self subviewsOfView:keyboard withType:@"UIKBKeyplaneView"]) {
                UIView *newView = [[UIView alloc] initWithFrame:[(UIView *)[[self subviewsOfView:keyboard withType:@"UIKBKeyView"] lastObject] frame]];
                newView.frame = CGRectMake(newView.frame.origin.x + 2, newView.frame.origin.y + 1, newView.frame.size.width - 4, newView.frame.size.height -3);
                [newView setBackgroundColor:[UIColor greenColor]];
                newView.layer.cornerRadius = 4;
                [view insertSubview:newView belowSubview:((UIView *)[[self subviewsOfView:keyboard withType:@"UIKBKeyView"] lastObject])];

            }
        }
    }
}

The app I used to decode the view hierarchy was : http://revealapp.com/

The end result is like this:




回答2:


You can not change button tint color but you can set keyboard Tint color by using UIKeyboardAppearance

Example: yourTextField.keyboardAppearance = UIKeyboardAppearanceDark;

Here is a very nice document provided by Apple, take a look here:

Managing the Keyboard




回答3:


let colors: [UIColor] = [.red, .blue, .green, .purple, .yellow, .orange, .brown]

if let window = UIApplication.shared.windows.first(where: { 
    $0.isType(string: "UIRemoteKeyboardWindow") 
}) {
    if let keyplaneView = window.subview(ofType: "UIKBKeyplaneView") {
        for (i, keyView) in keyplaneView.subviews.filter({
            $0.isType(string: "UIKBKeyView") 
        }).enumerated() {
            let view = UIView(frame: keyView.bounds)
            view.backgroundColor = colors[i].withAlphaComponent(0.5)
            keyView.addSubview(view)
        }
    }
}

Here is a color map of the keys in the UIKBKeyplaneView:



来源:https://stackoverflow.com/questions/19465765/ios7-keyboard-return-done-search-tint-colour

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