【问题标题】:UIPickerView selection indicator not visible in iOS10UIPickerView 选择指示器在 iOS10 中不可见
【发布时间】:2017-01-26 15:22:54
【问题描述】:

我在 Xcode 8 中构建我的项目。UIPickerView 分隔线在 iOS 10 模拟器和设备中不可见,但在 iOS 9.3 设备和模拟器上工作正常。我试图在 XIB 中调整 UIPickerView 背景颜色、自动布局和所有可能的东西,但没有任何效果。有人对此有想法吗?

这是一个包含 UIPickerView 的自定义视图

-(void)layoutSubviews{
    isShown = NO;
    [super layoutSubviews];
    
    //self.selectedDic = nil;
    
    self.doneBtn.tintColor = COLOR_DB3535;
    self.pickerView.backgroundColor = COLOR_DEDEDE;
    self.pickerView.showsSelectionIndicator = YES;
    
    [self.doneBtn setTitle:NSLocalizedString(@"App_Generic_Button_Text_Done", @"")];
    }


    -(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    label.tintColor = [UIColor clearColor];
    label.backgroundColor = [UIColor yellowColor];
    label.textColor = COLOR_666;
    label.font = [FontsManager getFONT_ROBOTO_LIGHT_16];
    label.textAlignment = NSTextAlignmentCenter;
    NSDictionary *dict = [dataArray objectAtIndex:row];
    label.text = @"Test";
    return label;
}

【问题讨论】:

标签: ios objective-c uipickerview ios10 xcode8


【解决方案1】:

我不确定您在说什么“分隔线”。我在 iOS 9 中也没有看到“分隔线”。

您的屏幕截图中唯一缺少的“线条”是选择指示器。您可以通过将选择器视图的showsSelectionIndicator 设置为YES 来获取它们。但你不应该这样做;显示选择指示器是默认设置。文档说:

在 iOS 7 及更高版本...始终显示选择指示器

【讨论】:

  • 是的,我很抱歉。应作为选择指标加以纠正。我编辑了问题
  • 当然,部分问题可能是您的背景与选择指示器的颜色相同。所以线条可能在那里,但你看不到它们。我不知道。但是我的屏幕截图是在 iOS 10 中拍摄的,因此证明可以获取选择指示线。
  • elf.pickerView.backgroundColor = [UIColor yellowColor]; self.pickerView.showsSelectionIndicator = YES;但我仍然看不到选择指示器。奇怪的问题
  • 好吧,我看不到你的代码。你想看看我的吗?这是一个工作示例:github.com/mattneub/Programming-iOS-Book-Examples/tree/master/… 但我不必明确设置showsSelectionIndicator;默认是YES。
  • 您真的在设备上尝试过吗?我想你只是在模拟器中尝试过,你的模拟器设置有问题。
【解决方案2】:

我也面临同样的问题。下面是我创建 UIPickerView 的代码:

self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, frame.size.height - 216, frame.size.width, 216)];
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
self.pickerView.backgroundColor = [UIColor whiteColor];
self.pickerView.showsSelectionIndicator = YES;
[self addSubview:self.pickerView];

【讨论】:

    【解决方案3】:

    我在 iOS10 中也遇到过同样的问题。这是我的问题:

    我通过以下方式解决了这个问题:

     self.pickerView.backgroundColor = COLOR_DEDEDE;
     self.pickerView.showsSelectionIndicator = YES;
        for (UIView *view in self.pickerView.subviews) {
            if (view.bounds.size.height < 2.0f && view.backgroundColor == nil) {
                view.backgroundColor = PICK_Line_COLOR; // line color
            }
        }
    

    注意:

    此代码必须在方法之后调用:[self.view addSubview:pickeView];

    最终结果:

    它适用于我的项目。希望对你有帮助。

    【讨论】:

      【解决方案4】:

      当我为 iOS10 重建几个解决方案并在模拟器和设备上部署到 iOS10 时,我遇到了这个确切的问题。

      我将我的问题缩小到在初始化期间在选择器中选择一个项目。 IE。我填充了我的选择器,如果我们已经选择了这个属性,那么我预先选择了它并且线条存在。当我设置我的选择器时,我会在初始化期间执行此操作。

      因此,在我的用例中有效的解决方法是在没有现有值的情况下选择 0 元素。

      Objective-C

      UIPickerView *pickerView;
      
      ...
      
      [pickerView selectRow:0 inComponent:0 animated:YES];
      

      斯威夫特

      let pickerView: UIPickerView
      
      ...
      
      pickerView.selectRow(0, inComponent: 0, animated: true)
      

      这对我的解决方案来说很好,因为我在设置时有效地选择了第 0 行。

      我还没有机会深入研究推理或寻找更简洁的解决方案,但既然我已经解决了我的问题,我想如果可以的话,我会在这里分享它以帮助大家。

      【讨论】:

      • 在初始化期间添加选择代码确实解决了我的问题。谢谢!
      • 好吧,这对我不起作用。事实上,我已经在这样做了。我必须在初始化时选择一些特定的值。
      【解决方案5】:

      这是我的代码:

      -(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
      
          for(UIView *single in pickerView.subviews)
          {
              if (single.frame.size.height < 1)
              {
                  single.backgroundColor = [UIColor grayColor];
              }
          }
         //other code
      
      }
      

      【讨论】:

        【解决方案6】:
        UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 200)];
        pickerView.backgroundColor = [UIColor whiteColor];
        pickerView.dataSource = self;
        pickerView.delegate = self;
        [pickerView selectRow:0 inComponent:0 animated:YES];
        [self.view addSubview:pickerView];
        

        在将pickerView添加到superView之前添加代码[pickerView selectRow:0 inComponent:0 animated:YES];。

        【讨论】:

        • 完美。在 IOS 10 中,这条线再次出现。
        • 也适用于 iOS 11。谢谢!
        • 还为我修复了 iOS 12 中的错误。苹果怎么还没解决这个问题??
        • 也为我修复了 iOS 12 中的错误。
        【解决方案7】:

        使用 UIPickerView 的子类解决了这个问题:

        import UIKit
        
            class FixedPickerView: UIPickerView, UIPickerViewDelegate {
        
                override func willMove(toSuperview newSuperview: UIView?) {
                    self.delegate = self
                    self.selectRow(0, inComponent: 0, animated: true)
                    self.delegate = nil
                    super.willMove(toSuperview: newSuperview)
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多