How to get UIPickerView object referee when creating via html select tag

My UIWebview has an html "select" tag, which is displayed like dropdown liston the screen.

When I click the drop-down list, UIWebview automatically displays the UIWebSelectSinglePicker view, which displays as default OS pickerview.

I want to change the background color of the color picker and the color of the text. How can I achieve this?

I tried to listen to the UIKeyboardWillShowNotification event, but at that moment this view was not created.

Thanks in advance for your help.

+4
source share
2 answers

I managed to solve the problem myself.

- UIPickView , :

UIKeyboardWillShowNotification.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_pickerViewWillBeShown:) name:UIKeyboardWillShowNotification object:nil];

-, , . < - , , pickview .

- (void)_pickerViewWillBeShown:(NSNotification*)aNotification {
   [self performSelector:@selector(_resetPickerViewBackgroundAfterDelay) withObject:nil   afterDelay:0];
}

-, UIApplication pickerView. , pickerView.

-(void)_resetPickerViewBackgroundAfterDelay
{
  UIPickerView *pickerView = nil;
  for (UIWindow *uiWindow in [[UIApplication sharedApplication] windows]) {
    for (UIView *uiView in [uiWindow subviews]) {
      pickerView = [self _findPickerView:uiView];
    }
  }

  if (pickerView){
    [pickerView setBackgroundColor:UIColorFromRGB(0x00FF00)];
  }
}

(UIPickerView *) _findPickerView:(UIView *)uiView {
  if ([uiView isKindOfClass:[UIPickerView class]] ){
    return (UIPickerView*) uiView;
  }
  if ([uiView subviews].count > 0) {
    for (UIView *subview in [uiView subviews]){
      UIPickerView* view = [self _findPickerView:subview];
      if (view)
        return view;
    }
  }
  return nil;
}

, .

+3

, . , ( ). .

, swizzling, ( , , ).

UIWebSelectSinglePicker ( ), UIPickerViewDelegate. NSAttributedString, - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component. , , .

, UIPickerView:

@implementation UIPickerView (LabelColourOverride)

- (NSAttributedString *)overridePickerView:(UIPickerView *)pickerView 
                     attributedTitleForRow:(NSInteger)row 
                              forComponent:(NSInteger)component
{
    // Get the original title
    NSMutableAttributedString* title = 
        (NSMutableAttributedString*)[self overridePickerView:pickerView 
                                       attributedTitleForRow:row 
                                                forComponent:component];

    // Modify any attributes you like. The following changes the text colour.
    [title setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} 
                   range:NSMakeRange(0, title.length)];

    // You can also conveniently change the background of the picker as well. 
    // Multiple calls to set backgroundColor doesn't seem to slow the use of 
    // the picker, but you could just as easily do a check before setting the
    // colour to see if it needed.
    pickerView.backgroundColor = [UIColor yellowColor];

    return title;
}

@end

, swizzling (. ), :

[Swizzle swizzleClass:NSClassFromString(@"UIWebSelectSinglePicker") 
               method:@selector(pickerView:attributedTitleForRow:forComponent:) 
               forClass:[UIPickerView class] 
               method:@selector(overridePickerView:attributedTitleForRow:forComponent:)];

Swizzle, .

@implementation Swizzle

+ (void)swizzleClass:(Class)originalClass 
              method:(SEL)originalSelector 
            forClass:(Class)overrideClass 
              method:(SEL)overrideSelector
{
    Method originalMethod = class_getInstanceMethod(originalClass, originalSelector);
    Method overrideMethod = class_getInstanceMethod(overrideClass, overrideSelector);
    if (class_addMethod(originalClass, 
                        originalSelector,
                        method_getImplementation(overrideMethod),
                        method_getTypeEncoding(overrideMethod))) {
        class_replaceMethod(originalClass,
                            overrideSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } 
    else {
        method_exchangeImplementations(originalMethod, overrideMethod);
    }
}

@end

, , , , , , NSAttributedString, . , . , .

Picker , , ( ).

+1

All Articles