jeudi 29 janvier 2015

Changing application's nib in swift on device orientation change

So I've been looking around for how to handle xib changes in swift (specifically on orientation change) and am coming up pretty empty handed.


I attempted to modify the obj-c code I found here but it's pretty different:


Want to use muliple nibs for different iphone interface orientations


What I'm trying to do is:




  1. Load the appropriate nib on application load.




  2. On orientation change (from landscape to portrait or vice-versa), load the nib associated with that orientation.




I found this and started modifying it - still don't quite have it but seems like a better direction than the one I was headed in:



override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
var text=""
switch UIDevice.currentDevice().orientation{
case .Portrait:
text="Portrait"
case .PortraitUpsideDown:
text="PortraitUpsideDown"
case .LandscapeLeft:
text="LandscapeLeft"
case .LandscapeRight:
text="LandscapeRight"
default:
text="Another"
}
NSLog("You have moved: \(text)")
}


I think I know how to read orientation changes but I still haven't figured out the nib switching yet.


//below is old


Here's what I currently have (I don't really understand how to format UIInterfaceOrientationIsPortrait ATM - so it's broken)



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

func loadInterface(){

if (UIInterfaceOrientationIsPortrait == true){
keyboardNib = UINib(nibName: "keyboardView", bundle: nil)
}
else{
keyboardNib = UINib(nibName: "keyboardViewPortrait", bundle: nil)
}

self.keyboardView = keyboardNib.instantiateWithOwner(self, options: nil)[0] as UIView

view.addSubview(self.keyboardView)

view.backgroundColor = self.keyboardView.backgroundColor

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

}


(Oh and the reason for the xib change is that the landscape and portrait views have different images that would scale strangely, and the proportions are different)




Aucun commentaire:

Enregistrer un commentaire