2013-10-30 15 views
4

我有一个UIWebview包含一个html“select”标签,它在屏幕上显示为dropdown list如何通过html选择标签创建UIPickerView的对象引用

当我点击下拉菜单时,UIWebview会自动弹出一个UIWebSelectSinglePicker视图,显示为default OS pickerview

我想更改选取器视图的背景颜色和文本颜色。我怎样才能达到这个目标?

我试着听UIKeyboardWillShowNotification事件,但那一刻,这个视图还没有被创建。

在此先感谢您的帮助。

回答

3

我设法自己解决了这个问题。

如果有人也想改变上飞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; 
} 

希望它能帮上忙。

1

我相信我已经想出了一个替代解决方案来解决这个问题。在标签颜色显示不正确的情况下(使用系统默认值而不是覆盖的颜色)提出了另一种解决方案。滚动项目列表时发生这种情况。

为了防止这种情况发生,我们可以使用方法调整来修复标签颜色的来源(而不是在它们已经创建之后对其进行修补)。

显示的UIWebSelectSinglePicker(如您所述)实现了UIPickerViewDelegate协议。该协议负责提供通过- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component方法显示在选取器视图中的NSAttributedString实例。通过与我们自己的实现混合,我们可以重写标签的外观。

要做到这一点,我定义的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's needed. 
    pickerView.backgroundColor = [UIColor yellowColor]; 

    return title; 
} 

@end 

然后使用方法交叉混合(see this answer for reference),我们交换的实现:

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

这是调酒实现我开发了基于关闭上面的链接。

@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我们可以反正我们要修改英寸如果我们想要,我们可以完全替换返回值,只保留文本。 Find the list of attributes you can change here

该解决方案允许您通过单个调用全局更改应用中的所有Picker视图,无需为需要此代码的每个视图控制器注册通知(或定义基类来执行相同操作)。

相关问题