IOS7 UIPickerView how to hide the selection indica

2019-01-17 02:58发布

How can I hide those 2 lines on the selected row?

enter image description here

15条回答
Explosion°爆炸
2楼-- · 2019-01-17 03:39

This code works fine for iOS 10 with swift 3

Just add this code in your view controller class.

override func viewDidLayoutSubviews() {
        timePickerView.subviews[1].isHidden = true
        timePickerView.subviews[2].isHidden = true
        }
查看更多
唯我独甜
3楼-- · 2019-01-17 03:40

You can also make an extension to UIPickerView:

extension UIPickerView {
    func hideSelectionIndicator() {
        for i in [1, 2] {
            self.subviews[i].isHidden = true
        }
    }
}

And then just call myPickerView.hideSelectionIndicator() for each PickerView you want to alter.

查看更多
Ridiculous、
4楼-- · 2019-01-17 03:43

Swift 3 Version (Working):

pickerView.subviews[1].isHidden = true
pickerView.subviews[2].isHidden = true
查看更多
该账号已被封号
5楼-- · 2019-01-17 03:46

Based on the other answers, I decided to enumerate the subviews and saw that the lines have a height of 0.5 so my solution now looks like this in Swift:

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

     pickerView.subviews.forEach({

          $0.hidden = $0.frame.height < 1.0
     })

     return myRowCount
}

And in Objective-C

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

     [pickerView.subviews enumerateObjectsUsingBlock:^(UIView *subview, NSUInteger idx, BOOL *stop) {

        subview.hidden = (CGRectGetHeight(subview.frame) < 1.0) 
     }];

    return myRowCount
}

Obviously not particularly future proof, but probably more so than hiding a subview at a given index.

Edit: Updated to handle the case provided by @Loris

查看更多
6楼-- · 2019-01-17 03:47

Swift 4.2

Paste both lines of code into either your titleForRow or viewForRow delegate method of the pickerView.

pickerView.subviews[1].isHidden = true
pickerView.subviews[2].isHidden = true

And you should be good to go.

查看更多
时光不老,我们不散
7楼-- · 2019-01-17 03:48
func numberOfComponents(in pickerView: UIPickerView) -> Int
    {
        pickerView.subviews.forEach({
            $0.isHidden = $0.frame.height < 1.0
        })
        return 1
    }
查看更多
登录 后发表回答