UIPickerView selection indicator not visible in iOS10

前端 未结 7 2039
刺人心
刺人心 2021-02-04 06:01

I build my project in Xcode 8. UIPickerView separator lines are not visible in iOS 10 simulator and the devices, but works fine on iOS 9.3 devices and simulator. I tried to adju

相关标签:
7条回答
  • 2021-02-04 06:28

    This is my code:

    -(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
    
    }
    
    0 讨论(0)
  • 2021-02-04 06:31

    I had this exact issue when I rebuilt a couple of solutions for iOS10 and deployed to iOS10 on both simulators and devices.

    I narrowed the problem in my case to be down to selecting an item in the picker during initialisation. ie. I populate my picker and if we have already got a selection for this property then I preselect it and the lines are present. I do this during the initialisation when I set up my picker.

    So my fix, which worked in my use case, was to select the 0 element in the case of no existing value.

    Objective-C

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

    Swift

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

    This was fine for my solution since I effectively select row zero on setup.

    I haven't had chance to dig into the reasoning or look for a cleaner solution, but since I've solved my problem I thought I'd share it here to help you all out if I can.

    0 讨论(0)
  • 2021-02-04 06:32
    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];
    

    add the code [pickerView selectRow:0 inComponent:0 animated:YES]; before pickerView added to superView.

    0 讨论(0)
  • 2021-02-04 06:35

    I'm not sure what "separator lines" you are talking about. I don't see "separator lines" in iOS 9 either.

    The only "lines" missing from your screen shot are the selection indicator. You can get them by setting the picker view's showsSelectionIndicator to YES. But you shouldn't have to; showing the selection indicator is the default. The docs say:

    On iOS 7 and later ... the selection indicator is always shown

    0 讨论(0)
  • 2021-02-04 06:46

    I am also facing the same issue. Below is my code for creating the 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];
    
    0 讨论(0)
  • 2021-02-04 06:48

    Solved this problem using subclass of 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)
            }
        }
    
    0 讨论(0)
提交回复
热议问题