问题
How can I hide those 2 lines on the selected row?
回答1:
[[pickerview.subviews objectAtIndex:1] setHidden:TRUE];
[[pickerview.subviews objectAtIndex:2] setHidden:TRUE];
Use this in titleForRow
or viewForRow
delegate method of the pickerView
.
回答2:
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
回答3:
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.
回答4:
This worked for me in Swift in iOS 9 Beta.
datePicker.subviews[0].subviews[1].hidden = true
datePicker.subviews[0].subviews[2].hidden = true
回答5:
Swift 3 Version (Working):
pickerView.subviews[1].isHidden = true
pickerView.subviews[2].isHidden = true
回答6:
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
pickerView.subviews.forEach({
$0.isHidden = $0.frame.height < 1.0
})
return 1
}
回答7:
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
回答8:
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.
回答9:
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.
回答10:
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.
回答11:
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!
回答12:
Just write this code in your viewdidload method
[[PickerView.subviews objectAtIndex:1] setHidden:TRUE];
[[PickerView.subviews objectAtIndex:2] setHidden:TRUE];
回答13:
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 :)
回答14:
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.
回答15:
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
}
来源:https://stackoverflow.com/questions/20612279/ios7-uipickerview-how-to-hide-the-selection-indicator