简体   繁体   中英

loading a xib view on orientation change in swift

I'm trying to get my keybordview to switch on orientation change - I have my views within my .xib hooked up with @IBOutlet then I call them with loadInterface() inside of viewDidLoad() I was hoping that my call in viewDidLoad would update the interface but I don't think it's working quite how I thought - it doesn't break anything but it doesn't work either (though, I've made tons of progress from last week.).

I have a feeling it's something with the orientation change...

import UIKit

class KeyboardViewController: UIInputViewController {

@IBOutlet var keyboardViewPortrait: UIView!
@IBOutlet var keyboardViewLandscape: UIView!

func loadInterface(){
    var keyboardNib = UINib(nibName: "keyboardView", bundle: nil)

    UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()

    if self.interfaceOrientation.isLandscape {
        self.keyboardViewLandscape = keyboardNib.instantiateWithOwner(self, options: nil)[0] as UIView
        view.addSubview(self.keyboardViewLandscape)
        view.backgroundColor = self.keyboardViewLandscape.backgroundColor
    }
    else if self.interfaceOrientation.isPortrait {
        self.keyboardViewPortrait = keyboardNib.instantiateWithOwner(self, options: nil)[0] as UIView
        view.addSubview(self.keyboardViewPortrait)
        view.backgroundColor = self.keyboardViewPortrait.backgroundColor
    }

    self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)       
}

override func viewDidLoad() {
    super.viewDidLoad()
    UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
    loadInterface()

    ...
}
}

*update

I think rdelmar set me on the right path but something is still missing.

thanks!

The problem is that your assigning the same view to self.keyboardViewLandscape, and self.keyboardViewPortrait (in both cases that's the view you get from keyboardNib.instantiateWithOwner(self, options: nil)[0]). One of those properties should be assigned to the object at index 1. Also, it might be better to use the new method for dealing with rotation, viewWillTransitionToSize:withTransitionCoordinator:. You also need to remove the previous view when you add the new one.

class ViewController: UIViewController {

    var keyboardViewPortrait: UIView!
    var keyboardViewLandscape: UIView!
    var keyboardNib = UINib(nibName: "keyboardView", bundle: nil)

    override func viewDidLoad() {
        super.viewDidLoad()
        self.keyboardViewPortrait = keyboardNib.instantiateWithOwner(self, options: nil)[0] as UIView
        view.addSubview(self.keyboardViewPortrait)
        view.backgroundColor = self.keyboardViewPortrait.backgroundColor
    }

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator){

        if size.width > size.height {
            self.keyboardViewPortrait.removeFromSuperview()
            self.keyboardViewLandscape = keyboardNib.instantiateWithOwner(self, options: nil)[1] as UIView
            view.addSubview(self.keyboardViewLandscape)
            view.backgroundColor = self.keyboardViewLandscape.backgroundColor
        }
        else {
            self.keyboardViewLandscape.removeFromSuperview()
            self.keyboardViewPortrait = keyboardNib.instantiateWithOwner(self, options: nil)[0] as UIView
            view.addSubview(self.keyboardViewPortrait)
            view.backgroundColor = self.keyboardViewPortrait.backgroundColor
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM