制作水平 UIPickerView
Posted
技术标签:
【中文标题】制作水平 UIPickerView【英文标题】:Making Horizontal UIPickerView 【发布时间】:2018-03-17 08:48:23 【问题描述】:我正在尝试制作一个水平 UIPickerView。但我对宽度有一些问题。正如你可以在上图的顶部看到
我希望 PickerView 的宽度为 = view.frame.size.width,但由于某种原因它保持非常小。在我的代码中,我有:
let cityPicker = UIPickerView()
var rotationAngle: CGFloat!
let width:CGFloat = 100
let height:CGFloat = 100
override func viewDidLoad()
super.viewDidLoad()
cityPicker.delegate = self
cityPicker.dataSource = self
cityPicker.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 50)
self.view.addSubview(cityPicker)
rotationAngle = -90 * (.pi/180)
cityPicker.transform = CGAffineTransform(rotationAngle: rotationAngle)
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return byer.count
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat
return 100
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat
return 100
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView
let view = UIView()
view.frame = CGRect(x: 0, y: 0, width: width, height: height)
let label = UILabel()
label.frame = CGRect(x: 0, y: 0, width: width, height: height)
label.textAlignment = .center
label.font = UIFont (name: "Baskerville-Bold", size: 30)
label.text = byer[row]
view.addSubview(label)
view.transform = CGAffineTransform(rotationAngle: 90 * (.pi/180))
return view
谁能看到我做错了什么?
【问题讨论】:
【参考方案1】:您应该只使用自动布局,但如果您想要手动布局,则不能在viewDidLoad
中使用您的视图框架。此时框架将等于故事板或 nib 文件中的内容,并且不会反映实际的设备或视图层次结构。您应该将手动调整大小逻辑放在viewDidLayoutSubviews
中。调用此函数时,您的视图具有正确的大小,您可以使用它来设置子视图。
【讨论】:
【参考方案2】:var rotationAngle : CGFloat!
在您的 ViewDidLoad 中
rotationAngle = -90 * (.pi / 180 )
在你的课堂上写这个函数
func rotatePickerView(pickerView : UIPickerView)
let y = pickerView.frame.origin.y
let x = pickerView.frame.origin.x
pickerView.transform = CGAffineTransform(rotationAngle: rotationAngle)
pickerView.frame = CGRect(x: x, y: y, width: pickerView.frame.height , height: pickerView.frame.width)
我们还需要在旋转我们的pickerview时旋转标签,现在在你的viewForRow方法中。 (这些是 Pickerview 委托方法。)
let label = UILabel()
label.font = UIFont(name: "Helvetica", size: 40)
label.font = UIFont.systemFont(ofSize: 40, weight: .bold)
label.minimumScaleFactor = 0.5
label.textAlignment = .center
label.textColor = UIColor.white
label.transform = CGAffineTransform(rotationAngle: 90 * (.pi / 180 ))
//Put your values in an array like Minutes,Temperature etc.
label.text = minutes[row]
return label
在你的viewDidLoad中调用rotatePickerView函数,把你的pickerview作为参数输入,Done,谢谢;)
【讨论】:
【参考方案3】:我不知道@Josh Homann 所说的“只使用自动布局”是什么意思,但我必须创建一个旋转选择器
private let horizontalPickerView: UIPickerView =
let view = UIPickerView()
view.translatesAutoresizingMaskIntoConstraints = false
view.showsSelectionIndicator = false
view.transform = CGAffineTransform(rotationAngle: -90 * (.pi / 180))
return view
()
然后将其约束设置为经验计算
horizontalPickerView.widthAnchor.constraint(equalToConstant: screenWidth),
horizontalPickerView.heightAnchor.constraint(equalToConstant: 300),
horizontalPickerView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
horizontalPickerView.centerYAnchor.constraint(equalTo: mainButton.topAnchor, constant: -32)
并在 UIPickerViewDelegate 方法中设置项目宽度(实际上是高度)
public func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat
100
得到这个
【讨论】:
如何把它变平? 可能通过 UIScrollView以上是关于制作水平 UIPickerView的主要内容,如果未能解决你的问题,请参考以下文章