ios - 以编程方式创建一个在横向和纵向工作的 UIPickerView

标签 ios swift ios9 uipickerview screen-rotation

我想创建一个最小的 UIPickerView,它在纵向和横向格式下看起来都不错,而且它的工作......有点。 我在按下按钮后添加了 pickerView,完成后将其隐藏。 pickerView 正常工作:

  • 当我以纵向或横向启动应用程序并在不改变方向的情况下按下按钮时
  • 当我在屏幕上显示 pickerView 时,更改方向可以正常工作,pickerView 会更改大小

但是当以一个方向启动并在单击按钮显示 pickerView 之前改变方向时它不能很好地工作,然后它会显示这种东西: in portait orientation in paysage orientation

这是代码(单 View 应用程序, Storyboard中只有一个按钮:myButton,带有触摸操作 touchMyButton)

class myViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var myButton: UIButton!
var myPickerView: UIPickerView! = UIPickerView(frame:CGRectMake(0.0, 0.0, UIScreen.mainScreen().bounds.width, 162))
var activePickerView: Int = 0

override func viewDidLoad() {
    super.viewDidLoad()
    myPickerView.tag = 1
    myPickerView.delegate = self
    myPickerView.autoresizingMask = [.FlexibleWidth]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


@IBAction func touchMyButton(sender: AnyObject) {
    if (activePickerView == 0)
    {
        activePickerView = myPickerView.tag
        myPickerView.selectRow(5, inComponent:0, animated:false)
        myPickerView.backgroundColor = UIColor.init(white:0.80, alpha:0.80)
        myPickerView.frame = CGRectMake(0.0, 100.0, UIScreen.mainScreen().bounds.width, 0.0)
        self.view.layoutIfNeeded()
        self.view.addSubview(myPickerView)
        myPickerView.setNeedsDisplay()

        UIView.animateWithDuration(0.5, delay:0.0, options:UIViewAnimationOptions.CurveEaseInOut, animations:{
            self.view.layoutIfNeeded()
            self.myPickerView.frame = CGRectMake(0.0, 100.0, self.myPickerView.frame.width, 162)
            }, completion: { _ in
        })
    }
    else
    {
        self.selectPickerView()
    }
}

func selectPickerView(){
    if (activePickerView == myPickerView.tag)
    {
        let _ = myPickerView.selectedRowInComponent(0)
        let framePV = self.myPickerView.frame

        UIView.animateWithDuration(0.5, delay:0.0, options:UIViewAnimationOptions.CurveEaseInOut, animations:{
            self.view.layoutIfNeeded()
            self.myPickerView.frame = CGRectMake(0.0, framePV.origin.y, framePV.width, 0.0 )
            }, completion:{ _ in
                self.myPickerView.removeFromSuperview()
        })
    }
    activePickerView = 0
}

// MARK: UIPickerViewDataSource methods
func numberOfComponentsInPickerView(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 \(row)" }

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    activePickerView = pickerView.tag
    selectPickerView()
}


override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation)
{   self.view.layoutIfNeeded()  }
}

我迷路了,我看不出我可以改变什么来让它发挥作用

最佳答案

谢谢,我是有约束力的(就是不太简单),就这样

import UIKit

class myViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var myButton: UIButton!
var myPickerView: UIPickerView! = UIPickerView()
var activePickerView: Int = 0

override func viewDidLoad() {
    super.viewDidLoad()
    myPickerView.tag = 1
    myPickerView.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

@IBAction func touchMyButton(sender: AnyObject) {
    if (activePickerView == 0){
        activePickerView = myPickerView.tag
        myPickerView.selectRow(5, inComponent:0, animated:false)
        myPickerView.backgroundColor = UIColor.init(white:0.80, alpha:0.80)
        myPickerView.translatesAutoresizingMaskIntoConstraints = false

        self.view.addSubview(myPickerView)
        let margins = self.view.layoutMarginsGuide

        let topC = myPickerView.topAnchor.constraintEqualToAnchor(topLayoutGuide.topAnchor, constant: 100.0)
        let leadingC = myPickerView.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor)
        let trailingC = myPickerView.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor)
        let heightC = myPickerView.heightAnchor.constraintEqualToConstant(0.0)
        heightC.identifier = "height"
        NSLayoutConstraint.activateConstraints([topC, leadingC, trailingC, heightC])

        self.view.layoutIfNeeded()
        myPickerView.setNeedsLayout()
        heightC.constant = 162.0

        UIView.animateWithDuration(0.5, delay:0.0, options:UIViewAnimationOptions.CurveEaseInOut, animations:{
            self.view.layoutIfNeeded()
            }, completion: { _ in
        })
    }
    else {
        self.selectPickerView()
    }
}

func selectPickerView(){
    if (activePickerView == myPickerView.tag)
    {
        let _ = myPickerView.selectedRowInComponent(0)

        for constraint in myPickerView.constraints
        {
            if constraint.identifier == "height"
            {   constraint.constant = 0.0   }
        }
        myPickerView.setNeedsLayout()

        UIView.animateWithDuration(0.5, delay:0.0, options:UIViewAnimationOptions.CurveEaseInOut, animations:{
            self.view.layoutIfNeeded()
            }, completion:{ _ in
                self.myPickerView.removeConstraints(self.myPickerView.constraints)
                self.myPickerView.removeFromSuperview()
        })
    }
    activePickerView = 0
}

// MARK: UIPickerViewDataSource methods
func numberOfComponentsInPickerView(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 \(row)" }

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    activePickerView = pickerView.tag
    selectPickerView()
}

}

我不喜欢丢弃 pickerView 的动画(我不知道为什么它与显示它的动画不完全相反),但它有效。将来我可能会使用不透明 View 来更改它以覆盖 pickerView。

关于ios - 以编程方式创建一个在横向和纵向工作的 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34476378/

相关文章:

ios - 快速锁定 viewController 的方向

ios - 如何删除新 iTunes Connect 站点中的应用程序构建?

ios - NSUserDefaults 似乎没有创建全局可访问的变量

ios - 如何在 iOS 中快速使用 UDP 套接字?

ios - UIAlertAction 的处理程序有点太晚了——我怎样才能让它立即生效?

ios - 传输安全已阻止明文 HTTP

javascript - 没有新的苹果设备就不可能开发一个强制触摸网络应用程序吗?

ios - map 上的自定义图钉(注释)动画

ios - 如何在 Swift 中将速度设置为 "moveTo"方法?

ios - 名为 runner 的 SKSpriteNode 以相反的方式移动