为每种屏幕尺寸自动调整大小/调整UIPickerView的大小

时间:2016-01-28 08:45:29

标签: ios swift resize uipickerview size-classes

我正在尝试调整我的UIPickerView的大小,以及里面的内容,我已经调整了选择器视图的高度:

    let pickerView = UIPickerView()

    if DeviceType.IS_IPHONE_4_OR_LESS {
        pickerView.frame=CGRectMake(0, 200, view.frame.width, 100)
    } else if DeviceType.IS_IPHONE_5 {
        pickerView.frame=CGRectMake(0, 200, view.frame.width, 200)
    } else if DeviceType.IS_IPHONE_6 {
        pickerView.frame=CGRectMake(0, 200, view.frame.width, 200)
    } else if DeviceType.IS_IPHONE_6P {
        pickerView.frame=CGRectMake(0, 200, view.frame.width, 250)
    } else if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
        pickerView.frame=CGRectMake(0, 200, view.frame.width, 300)
    }

    //        pickerView.backgroundColor = .whiteColor()

    pickerView.showsSelectionIndicator = true
    pickerView.delegate = self
    pickerView.dataSource = self
    self.pickerView = pickerView

enter image description here

但是现在我想调整内部的内容,每行的高度,文本的大小,如果内容太长,请做多行,但我不知道该怎么做。

我的代码:

@IBOutlet var tfProjet: UITextField!
var pvProjetData = ["-Choisir-", "Rachat de crédits", "Renégociation de crédits"]
override func viewDidLoad() {

    super.viewDidLoad()

    let pickerView = UIPickerView()

    if DeviceType.IS_IPHONE_4_OR_LESS {
        pickerView.frame=CGRectMake(0, 200, view.frame.width, 100)
    } else if DeviceType.IS_IPHONE_5 {
        pickerView.frame=CGRectMake(0, 200, view.frame.width, 200)
    } else if DeviceType.IS_IPHONE_6 {
        pickerView.frame=CGRectMake(0, 200, view.frame.width, 200)
    } else if DeviceType.IS_IPHONE_6P {
        pickerView.frame=CGRectMake(0, 200, view.frame.width, 250)
    } else if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
        pickerView.frame=CGRectMake(0, 200, view.frame.width, 300)
    }

    //        pickerView.backgroundColor = .whiteColor()

    pickerView.showsSelectionIndicator = true
    pickerView.delegate = self
    pickerView.dataSource = self
    self.pickerView = pickerView

    tfProjet.inputView = pickerView
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
    return 1
}


func selectRow(row: Int, inComponent component: Int, animated: Bool){

}

func updatePicker(){
    self.pickerView!.reloadAllComponents()
    pickerView.selectRow(0, inComponent: 0, animated: false)
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    if tfProjet.isFirstResponder(){
        return pvProjetData.count
    }
    return 1
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    if tfProjet.isFirstResponder(){
        return pvProjetData[row]
    }
    return ""
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if tfProjet.isFirstResponder(){
        let itemselected = pvProjetData[row]
        tfProjet.text = itemselected
        self.tfProjet.endEditing(true)
    }
}

1 个答案:

答案 0 :(得分:0)

忘记通过代码执行此操作,使用autolayout。 Auytolayout与设备无关。