减少UIPickerView ios的内容范围

时间:2012-11-13 10:48:06

标签: ios uipickerview uipickerviewcontroller

由于前2个答案而进行的修改:我想要修改的内容不是UIPickerview的高度。我想要的是让UIPicker的内容从UIPicker的上方开始。这是一个例子: enter image description here

我想删除附加图片中显示的边距。I want to Kg(s) to be located at the upper border of UIPickerView, no the middle part

有什么想法吗?

使用工具栏,UIPickerView嵌入在UIView中。所以视图层次结构是:视图(父级) - >工具栏(子级),UIPickerView(子级)

View在viewController中声明为customPicker。 这是代码: 在 viewController.h

@interface myViewController:UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) IBoutlet UIView *customPicker;

@end

viewController.m

- (void) viewDidLoad{
self.customPicker.frame = CGRectMake(0, CGRectGetMaxY(self.view.frame), CGRectGetWidth(self.customPicker.frame), CGRectGetHeight(self.customPicker.frame));
    [self.view addSubview:self.customPicker];
}

然后我使用setPickerHidden方法为View显示动画以显示或隐藏它。

提前感谢您的帮助!

4 个答案:

答案 0 :(得分:2)

这是不可能的。如果您需要不同风格的“UIPickerView”,您将自己动手。

答案 1 :(得分:0)

您无法更改UIPickerView的高度。实际上,您只能修改选取器的宽度。更多信息here

答案 2 :(得分:0)

您可以使选择器从特定行(例如第2行或第3行)开始,以便最初不显示边距。然而,用户仍然可以向下滚动它,当它达到选择器内容的范围时,它仍然看起来像上面的例子。

或者你可以创建一个无限选择器视图的效果(虽然实际上它实际上只是一个有很多行的选择器视图)。

见这里: How do you make an UIPickerView component wrap around?

答案 3 :(得分:0)

2021 年答复

现在我们可以使用 UIPickerViewDelegate 创建一个没有边距的选择器。

Demo

让picker填充容器(蓝色边框),然后实现rowHeight委托方法并返回一个接近容器高度的值。完整代码在这里:

class PickerViewWrapper: UIView, UIPickerViewDataSource, UIPickerViewDelegate {
    
    // Make the return value of this delegate method object close to view height
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 60
    }
    
    let customPickerView = UIPickerView()
    let labelTexts = ["Day","Week","Month","Year"]
    
    init() {
        super.init(frame: .zero)
        customPickerView.dataSource = self
        customPickerView.delegate = self
        self.addSubview(customPickerView)
        

        // Let picker fill container view.
        customPickerView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            customPickerView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
            customPickerView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
            customPickerView.topAnchor.constraint(equalTo: self.topAnchor),
            customPickerView.bottomAnchor.constraint(equalTo: self.bottomAnchor)
        ])
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return labelTexts.count
    }

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let pickerLabel = UILabel()
        pickerLabel.text = labelTexts[row]
        pickerLabel.sizeToFit()
        return pickerLabel
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}