我正在尝试制作一个水平的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
}
谁能看到我做错了什么?
答案 0 :(得分:1)
你应该使用autolayout,但是如果你想要手动布局,你就无法在viewDidLoad
中使用你的视图框架。此时,框架将与storyboard或nib文件中的框架相同,并且不会反映实际的设备或视图层次结构。您应该将手动调整大小逻辑放在viewDidLayoutSubviews
中。调用此函数时,您的视图大小正确,您可以使用它来设置子视图。
答案 1 :(得分:0)
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作为参数输入完成,谢谢;)
答案 2 :(得分:0)
我不知道@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
}
得到这个