ios IOS7 UIPickerView 如何隐藏选择指示器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20612279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:47:42  来源:igfitidea点击:

IOS7 UIPickerView how to hide the selection indicator

iosuiviewios7uipickerview

提问by james075

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

如何隐藏所选行上的那两行?

enter image description here

在此处输入图片说明

回答by motox

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

Use this in titleForRowor viewForRowdelegate method of the pickerView.

使用 this intitleForRow或 的viewForRow委托方法pickerView

回答by ChrisH

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

根据其他答案,我决定枚举子视图并看到线条的高度为 ,0.5所以我的解决方案现在在 Swift 中看起来像这样:

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

     pickerView.subviews.forEach({

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

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

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

    return myRowCount
}
.hidden =
[[pickerview.subviews objectAtIndex:1] setHidden:TRUE];
[[pickerview.subviews objectAtIndex:2] setHidden:TRUE];
.frame.height < 1.0 }) return myRowCount }

And in Objective-C

在 Objective-C 中

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

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

编辑:更新以处理@Loris 提供的案例

回答by morksinaanab

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).

根据文档(https://developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UIPickerView.html),在 iOS7 中设置参数 pickerview.showsSelectionIndicator 无效。

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.

但是,由于最终的 UIPickerView 是带有子视图的 UIView,因此我检查了有哪些子视图。我找到了3个,第一个包含了UIPickerView的所有组件,另外两个是这两行。

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

因此,通过将第二个和第三个(索引 1 和 2)设置为隐藏,这两行就被删除了。

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

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

这不是一个真正好的解决方案,绝对不向前兼容,但现在它完成了工作。希望这可以帮助。

回答by Justin Domnitz

This worked for me in Swift in iOS 9 Beta.

这在 iOS 9 Beta 中的 Swift 中对我有用。

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

回答by ofeida

Swift 3 Version (Working):

Swift 3 版本(工作):

##代码##

回答by Larry Feldman

##代码##

回答by Dharmbir Singh

回答by HeavenlyManBR

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.

这很容易实现。只需将您的 PickerView 放在具有所需行大小的 ScrollView 中,然后使用选择器委托(pickerView:rowHeightForComponent:) 方法将选择器的行高更改为比您的 ScrollView 大一点。像这样,线条将被隐藏。

回答by Douglas Putnam

Swift 4.2

斯威夫特 4.2

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

将这两行代码粘贴到pickerView 的titleForRow 或viewForRow 委托方法中。

##代码##

And you should be good to go.

你应该很高兴去。

回答by Son Nguyen

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.

我通过一个简单的技巧解决了这个问题:在视图中放置选择器视图,并设置此视图的剪辑子视图属性 = true。现在,只需在选择器视图中设置行高 = 容器视图的高度,该行就会消失。