简体   繁体   中英

IOS7 UIPickerView how to hide the selection indicator

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

在此处输入图片说明

[[pickerview.subviews objectAtIndex:1] setHidden:TRUE];
[[pickerview.subviews objectAtIndex:2] setHidden:TRUE];

Use this in titleForRow or viewForRow delegate method of the pickerView .

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

In iOS7 setting the parameter pickerview.showsSelectionIndicator has no effect, according to the documentation ( https://developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UIPickerView.html ).

However, as a UIPickerView in the end is a UIView with subviews, I checked what subviews there were. I found 3, the first one contained all the components of the UIPickerView, and the other two are the two lines.

So by setting the second and third (index 1 and 2) hidden, the two lines were removed.

[[pickerview.subviews objectAtIndex:1] setHidden:TRUE];
[[pickerview.subviews objectAtIndex:2] setHidden:TRUE];

It's not a real nice solution, and definitely not forward compatible, but for now it gets the job done. Hope this helps.

This worked for me in Swift in iOS 9 Beta.

datePicker.subviews[0].subviews[1].hidden = true
datePicker.subviews[0].subviews[2].hidden = true

Swift 3 Version (Working):

pickerView.subviews[1].isHidden = true
pickerView.subviews[2].isHidden = true
func numberOfComponents(in pickerView: UIPickerView) -> Int
    {
        pickerView.subviews.forEach({
            $0.isHidden = $0.frame.height < 1.0
        })
        return 1
    }

It is working before ios7.

pickerView.showsSelectionIndicator = NO;

for more info in ios7 see this doc

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UIPickerView.html

This is easily achieved. Just place your PickerView inside a ScrollView with the desired size of your row, and use the picker delegate(pickerView:rowHeightForComponent:) method to change the the row height of the picker to a little bigger than your ScrollView. Like that, the lines will be hidden.

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.

Swift 5

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) ->  String? {
        pickerView.subviews[1].isHidden = true
        pickerView.subviews[2].isHidden = true
        return pickerData[row]
}

在此处输入图片说明

I solved this by a simple trick: Place picker view in a view, and set clip subviews property of this view = true. Now, just set height of row in picker view = height of container view then the line will disappear.

I opted for a different approach to make it a bit more future proof just in case Apple decide to change something down the line

for subview in setPickerView.subviews{
    if subview.frame.origin.y != 0{
        subview.isHidden = true
    }
}

since the (subView that contains the items)'s origin y location is 0 then I can safely hide anything else without risking an index out of bounds error

Enjoy

EDIT: I forgot to tell you that I put it in the viewDidLayoutSubviews method!

Just write this code in your viewdidload method

[[PickerView.subviews objectAtIndex:1] setHidden:TRUE];
[[PickerView.subviews objectAtIndex:2] setHidden:TRUE];

In ios7 we can't hidden the separate line in UIPickerView and we can know that from this page: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPickerView_Class/index.html#//apple_ref/occ/instm/UIPickerView/showsSelectionIndicator

But we can add two UIViews to cover it and the width of two UIViews is 1. Some sample code here:

s_leftLine = [[UIView alloc] initWithFrame:CGRectMake(s_pickerView.frame.size.height/2,
                                                      s_pickerView.frame.size.width/2 - kWidthOfPickerPage/2 + 1,
                                                      1,
                                                      s_pickerView.frame.size.height)];
s_leftLine.backgroundColor = [UIColor whiteColor];
s_leftLine.layer.zPosition = s_pickerView.layer.zPosition + 1; // make sure the line is on the top
[s_pickerView addSubview:s_leftLine];

Ok, this will be much better :] if someone has better answer just write it down for sharing :)

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
        }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM