Search code examples
iosswiftuipickerview

Can I hide a UIpicker and move a UIView in it's place?


Having a hell of a time fitting my app on a 4s, my strategy is that the user will select a picker, then when they type in the text field, the picker will disappear (the value having been selected and stored) and the textfields (inside "topView") pop up where the picker was to make room for the keyboard. I can see my topView try to pop, but it doesn't go anywhere. Can I do this? or is the picker's position above it stopping me, i.e. even though it's hidden, it's space is still there?

@IBOutlet weak var topHalfView: UIView!


override func viewDidLoad() {
    super.viewDidLoad()
    countyPicker.delegate = self
    countyPicker.dataSource = self

        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
    }

    func keyboardWillShow(sender: NSNotification) {
        countyPicker.hidden = true
         self.topHalfView.frame.origin.y += 150
    }

    func keyboardWillHide(sender: NSNotification) {
        countyPicker.hidden = false
        self.topHalfView.frame.origin.y -= 150

}

Solution

  • Turns out auto layout was what was preventing my view from popping. I'm not that comfortable having auto layout off, so if anyone knows a way to make it work with auto layout still on, please let me know.