Reputation: 1278
How can I hide those 2 lines on the selected row?
Upvotes: 37
Views: 24377
Reputation: 2901
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]
}
Upvotes: 3
Reputation: 182
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.
Upvotes: 4
Reputation: 95
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
pickerView.subviews.forEach({
$0.isHidden = $0.frame.height < 1.0
})
return 1
}
Upvotes: 7
Reputation: 1
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.
Upvotes: -3
Reputation: 125
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
}
Upvotes: -1
Reputation: 4558
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
Upvotes: 25
Reputation: 1341
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!
Upvotes: 2
Reputation: 139
Swift 3 Version (Working):
pickerView.subviews[1].isHidden = true
pickerView.subviews[2].isHidden = true
Upvotes: 7
Reputation: 3307
This worked for me in Swift in iOS 9 Beta.
datePicker.subviews[0].subviews[1].hidden = true
datePicker.subviews[0].subviews[2].hidden = true
Upvotes: 8
Reputation: 574
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 :)
Upvotes: 0
Reputation: 759
[[pickerview.subviews objectAtIndex:1] setHidden:TRUE];
[[pickerview.subviews objectAtIndex:2] setHidden:TRUE];
Use this in titleForRow
or viewForRow
delegate method of the pickerView
.
Upvotes: 37
Reputation: 1201
Just write this code in your viewdidload method
[[PickerView.subviews objectAtIndex:1] setHidden:TRUE];
[[PickerView.subviews objectAtIndex:2] setHidden:TRUE];
Upvotes: 0
Reputation: 3491
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.
Upvotes: 3
Reputation: 732
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.
Upvotes: 11
Reputation: 353
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.
Upvotes: 4
Reputation: 17535
It is working before ios7.
pickerView.showsSelectionIndicator = NO;
for more info in ios7 see this doc
Upvotes: 6