代码之家  ›  专栏  ›  技术社区  ›  Dmitry

如何使用SDK 7在iOS 7上设置UIPickerView的背景色?

  •  16
  • Dmitry  · 技术社区  · 12 年前

    如何设置的背景色 UIPickerView 在iOS 7上 使用SDK 7并使用标准选择器 在iOS 5和6上 ? 默认情况下,它在iOS 7上是透明的。

    6 回复  |  直到 12 年前
        1
  •  39
  •   Jesse    12 年前

    有什么问题:

    [picker setBackgroundColor:[UIColor whiteColor]];
    

    我假设您在调用picker视图时引用了它,并且它是UIView的一个子类,所以 backgroundColor 是有效的属性。。。

        2
  •  9
  •   Yanchi    12 年前

    我想把它写下来作为评论,但很难阅读:)Sooo。。。。

    -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 44)]; // your frame, so picker gets "colored"
        label.backgroundColor = [UIColor lightGrayColor];
        label.textColor = [UIColor blackColor];
        label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
        label.text = [NSString stringWithFormat:@"%d",row];
    
        return label;
    }
    

    此外,它不一定是唯一的标签,我认为你也可以在那里插入其他子视图。。。 据我所知,它适用于iOS7 pic here

        3
  •  9
  •   arlomedia    12 年前

    这对我来说很有效,在iOS 7.1中:

    [[UIPickerView appearance] setBackgroundColor:[UIColor whiteColor];
    

    这将更改所有拾取器的颜色。如果你只想在iOS 7的设备上运行,你可以在它周围设置一个条件。

        4
  •  8
  •   Dmitry    12 年前

    我已添加 UIView 在下面 UIPickerView 带有代码:

    CGRect framePickerView = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 216);
    pickerView = [[[UIView alloc] initWithFrame:framePickerView] autorelease];
    pickerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:pickerView];
    [pickerView addSubview:picker];
    

    而是代码:

    [self.view addSubview:picker];
    
        5
  •  5
  •   AamirR    8 年前

    尽管我设定了 UIPickerView.backgorundColor ,但我的背景颜色很奇怪。

    移除 以下行修复了该问题:

    UITextField.keyboardAppearance = .dark
    

    或者只是重置 keyboardAppearance 回到默认值,如下所示:

    UITextField.keyboardAppearance = .default
    
        6
  •  3
  •   Diesel    9 年前

    对于任何在swift工作并可能使用多个拾音器的人:

    pickerView1.backgroundColor = UIColor.darkGrayColor()
    pickerView2.backgroundColor = UIColor.greenColor()
    

    X代码7.3

    推荐文章