【问题标题】:Decrease the margin of content of UIPickerView ios减少 UIPickerView ios 内容的边距
【发布时间】:2012-11-01 19:13:30
【问题描述】:

由于前 2 个答案的修改:我要修改的不是 UIPickerview 的高度。我想要的是使 UIPicker 的内容从 UIPicker 的上侧开始。这是一个例子:

我想删除此处所附图片中显示的边距。

有什么想法吗?

使用工具栏,UIPickerView 嵌入在 UIView 中。所以视图层次结构是:View(parent)->Toolbar(child), UIPickerView(child)

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 设置动画以显示或隐藏它。

提前感谢您的帮助!

【问题讨论】:

    标签: ios uipickerview uipickerviewcontroller


    【解决方案1】:

    这是不可能的。如果您需要不同样式的“UIPickerView”,您可以自行开发。

    【讨论】:

    • 我想你误解了我的问题。这不是我想要改变的高度。我已经更新了我的帖子。我希望这次更清楚。
    • 同样的答案也适用。如果选择器视图位于第一个项目上,除非您创建自己的控件,否则您将在顶部看到此边距。时间选择器的工作方式不同。
    【解决方案2】:

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

    【讨论】:

    • 我想你误解了我的问题。这不是我想要改变的高度。我已经更新了我的帖子。我希望这次更清楚。
    【解决方案3】:

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

    或者,您可以创建无限选择器视图的效果(尽管实际上它实际上只是一个包含很多行的选择器视图)。

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

    【讨论】:

      【解决方案4】:

      2021 年回答

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

      让选取器填充容器(蓝色边框),然后实现 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")
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-03-20
        • 1970-01-01
        • 2022-01-18
        • 2019-08-30
        • 2022-12-17
        • 1970-01-01
        • 1970-01-01
        • 2011-05-01
        • 1970-01-01
        相关资源
        最近更新 更多