2017-02-06 3 views
1

UIDatePicker과 같은 사용자 지정 선택기 컨트롤을 만들려고하지만 선택기의 모든 논리를 포함하는 다른 데이터 소스를 사용하고 대상 동작 동작을 허용하려고합니다.하위 선택기 UIControl을 사용하여 사용자 지정 선택기를 작성하십시오

이렇게하려면 UIControl을 서브 클래스 화하고 UIPickerView을 추가하고 피커 뷰의 데이터 소스를 설정하고 하위 클래스에 위임합니다. 내 문제는 견해의 크기 조정에 있습니다.

모든 기기에서 UIDatePicker으로 놀았으며 기본 높이는 216이며 기본 너비는 320입니다. 너비를 늘리면 피커 뷰가 늘어나지 않지만 높이를 늘리면 피커 뷰가 늘어납니다. 시스템에 맞추기 위해 피커의 크기를 동일하게 설정하고 싶습니다.

내 서브 클래스에 대해 nib 파일을 만들려고했습니다. 보기 크기를 320x216으로 조정하고 UIPickerView을 추가하고 IBOutlet으로 연결했습니다. init?(coder aDecoder: NSCoder)에 대한 내 구현에서는 데이터 소스와 피커의 위임을 설정했지만 연결 테스트로 인해 내 응용 프로그램을 실행할 때 UIPickerView은 여전히 ​​nil입니다.

목표는 init()을 사용하고 스토리 보드에서 UIDatePicker과 같이 할 수 있으므로 펜촉을 버리고 코드에 모든 내용을 캡슐화하려고했습니다. UIDatePicker과 똑같이 동작하도록보기를 설정하려면 어떻게해야합니까?

class FWHeightPicker: UIControl { 
    let pickerView: UIPickerView 

    convenience init() { 
     let size = CGSize(width: 320, height: 216) 
     let origin = CGPoint(x: 0, y: 0) 
     let frame = CGRect(origin: origin, size: size) 

     self.init(frame: frame) 
    } 

    override init(frame: CGRect) { 
     pickerView = UIPickerView(frame: frame) 

     super.init(frame: frame) 
     commonInit() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     let size = CGSize(width: 320, height: 216) 
     let origin = CGPoint(x: 0, y: 0) 
     let frame = CGRect(origin: origin, size: size) 

     pickerView = UIPickerView(frame: frame) 

     super.init(coder: aDecoder) 
     commonInit() 
    } 

    func commonInit() { 
     let topConstraint = NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: pickerView, attribute: .top, multiplier: 1, constant: 0) 
     let bottomConstraint = NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: pickerView, attribute: .bottom, multiplier: 1, constant: 0) 
     self.addConstraints([topConstraint, bottomConstraint]) 

     pickerView.delegate = self 
     pickerView.dataSource = self 
    } 
} 

extension FWHeightPicker: UIPickerViewDelegate, UIPickerViewDataSource { 
// Delegate/Datasource implementations 
} 

답변

0

당신은 당신의 FWHeightPicker에 하위 뷰로 pickerView을 추가해야합니다 :

여기에 (모든 것이 스토리 보드에서 사용하는 코드로 가지고 있기 때문에 펜촉을 사용하지 않고) 내 현재 시도이다. 그런 다음 에 대한 기본 AutoLayout 제약 조건을 원하는 제약 조건에 맞게 pickerView.translatesAutoresizingMaskIntoConstraints = false으로 놓아야합니다.

편의상 초기화 된 코드 만 사용했습니다. 편의상 init()에 정의 된 크기에 맞게 피커의 너비와 높이를 늘리거나 줄입니다.

class FWHeightPicker: UIControl, UIPickerViewDelegate, UIPickerViewDataSource { 

    let pickerView: UIPickerView 

    convenience init() { 

    let size = CGSize(width: 320, height: 216) 
    let origin = CGPoint(x: 0, y: 0) 
    let frame = CGRect(origin: origin, size: size) 

    self.init(frame: frame) 
    } 

    override init(frame: CGRect) { 

    pickerView = UIPickerView(frame: frame) 

    super.init(frame: frame) 
    commonInit() 
    } 

    required init?(coder aDecoder: NSCoder) { 

    pickerView = UIPickerView() 

    super.init(coder: aDecoder) 
    commonInit() 
    } 

    func commonInit() { 

    pickerView.delegate = self 
    pickerView.dataSource = self 

    addSubview(pickerView) 

    pickerView.translatesAutoresizingMaskIntoConstraints = false 

    let topConstraint = NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: pickerView, attribute: .top, multiplier: 1, constant: 0) 

    let bottomConstraint = NSLayoutConstraint(item: pickerView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0) 

    let leftConstraint = NSLayoutConstraint(item: self, attribute: .leading, relatedBy: .equal, toItem: pickerView, attribute: .leading, multiplier: 1.0, constant: 0) 

    let rightConstraint = NSLayoutConstraint(item: pickerView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: 0) 

    self.addConstraints([topConstraint, bottomConstraint, leftConstraint, rightConstraint]) 

    } 

    // Delegate/Datasource implementations 
    func numberOfComponents(in pickerView: UIPickerView) -> Int { 

    return 1 
    } 

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

    return 10 
    } 

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

    return "row" 
    } 
} 
관련 문제