Currently my App looks like this in portrait mode:
but when my iPad is in landscape mode I want my Icons look like this:
By now it looks like that in landscape mode:
I know how to rotate the icons but I don't know where I have to paste the code. I just want to look my app in landscape mode like the 2nd pic.
EDIT
It should look like this one:
you need to set observer using NSNotificationCenter following code
NSNotificationCenter.defaultCenter().addObserver(self, selector: "deviceOrientationChnaged", name: UIDeviceOrientationDidChangeNotification, object: nil)
in AppDelegate didFinishedLaunching method.
And in observer method you check for orientation
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
print("landscape")
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
print("Portrait")
}
You should manage it from Assets if your deployment target is equal or greater than 8.0. You can set different images for different size class. For example for Portrait mode in iphone you can set assset forCompact width Regular Height size class and for iphones in landscape you can set asset for Any width Compact Height size class.
Refer apple documentation for Customizing Assets for Size Classes
You need to rotate your yourimageview by 90 degree in landscape mode -
In AppDelegate.swift inside the "didFinishLaunchingWithOptions" function I put:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
and then inside the AppDelegate class I put the following function:
func rotated()
{
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
print("landscape")
//Rotate 90 degrees clockwise:
yourimageview1.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
//Rotate 90 degrees counterclockwise:
yourimageview2.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
print("Portrait")
}
}
Hope this helps anyone else!
Try as following in your ViewController.m
-(void)viewDidLoad
{
NSNotificationCenter.defaultCenter().addObserver(self, selector: "deviceOrientationChnaged", name: UIDeviceOrientationDidChangeNotification, object: nil)
}
- (void) deviceOrientationChnaged:(NSNotification *) notification
{
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
print("landscape")
yourimageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
//OR Rotate 90 degrees counterclockwise:
yourimageview.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
print("Portrait")
}
}
Hope this will help.
EDIT:
Please check my this question: LINK
Related
My google mapView is not rotating according to my device orientation. Wondering how can I make it rotate accordingly?
I'm using the following to detect orientation changes. The landscape is being detected but I want to be able to make the map rotate too. Any help is appreciated, thanks!
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.rotated), name: UIDevice.orientationDidChangeNotification, object: nil)
#objc func rotated() {
if UIDevice.current.orientation.isLandscape {
print("Landscape orientation")
} else {
print("Portrait orientation")
}
}
I need to a way to constantly monitor if the user has rotated the iPad.
UserDidRotate() {
if(orientation = portrait.upsideDown){
//
//Code that will Present View upside down...
//
}
else if(orientation = portrait){
//
//Code that will Present View right side up...
//
}
}
How can I check for orientation change and also manually present the view upside down for my Swift 3 app?
EDIT:
I have tried:
override func viewWillTransition(to size: CGSize, with coordinator:
UIViewControllerTransitionCoordinator){}
and that method is never hit.
Try:
self.view.transform = self.view.transform.rotated(by: CGFloat(M_PI))
I'm done with the auto-layout stuff in my iOS universal App, and it's working perfectly in portrait. However, I want the user to be able to rotate the device and play the game in landscape mode. The problem I'm facing is that I don't want the layout to change at all, and only change the controls of the game (sliding up the screen should make the player go up in both orientations).
Thing is, I don't know how to prevent orientation from changing the layout and at the same time be able to change behaviour based on the orientation. Do you guys have any idea how I could manage that?
Did found a way to do, for future reference, when an orientation is disabled, we still can access device orientation (and not interface orientation), and register a notification to act upon change.
class ViewController: UIViewController {
var currentOrientation = 0
override func viewDidLoad() {
super.viewDidLoad()
// Register for notification about device orientation change
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(deviceDidRotate(notification:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
// Remove observer on window disappears
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
if UIDevice.current.isGeneratingDeviceOrientationNotifications {
UIDevice.current.endGeneratingDeviceOrientationNotifications()
}
}
// That part gets fired on orientation change, and I ignore states 0 - 5 - 6, respectively Unknown, flat up facing and down facing.
func deviceDidRotate(notification: NSNotification) {
if (UIDevice.current.orientation.rawValue < 5 && UIDevice.current.orientation.rawValue > 0) {
self.currentOrientation = UIDevice.current.orientation.rawValue
}
}
}
How can I just rotate the buttons when my device is in portrait or landscape mode? I know something like that:
button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
But where I have to call it up in my code?
I don't want to set my device in landscape mode, I just want to rotate the icons when it should be in landscape mode.
Thanks in advance!
You should override func
viewWillTransitionToSize(_ size: CGSize, withTransitionCoordinator coordinator:UIViewControllerTransitionCoordinator)
And call transform there
Don't forget to assign CGAffineTransformIdentity when rotated back
More about rotation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIContentContainer_Ref/index.html#//apple_ref/occ/intfm/UIContentContainer/viewWillTransitionToSize:withTransitionCoordinator:
The best way to do this is viewDidLoad add this line of code bellow:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(rotate), name: UIDeviceOrientationDidChangeNotification, object: nil)
and then in function rotate in case of device orientation do some code like this:
func rotate(){
if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) {
//your code here in landscape mode
}
if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation){
//your code in portrait mode
}
}
this solution is simple and really easy to do.
I guess you know how to "rotate" the buttons already, so I'll just tell you how to know that the device has rotated.
In a view controller, override willRotateToInterfaceOrientation:
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
}
In the method body, you can check the value of toInterfaceOrientation to know which orientation the device is rotating to. For example:
switch toInterfaceOrientation {
case Portrait:
// some code here...
default:
break
}
So I have a button which when clicked calls a function which adds some custom subviews to the view. So what I want is when the device is in portrait mode show me 4 subviews and when you rotate it to landscape show me only one. I want to do something like this:
func showSubviews {
if deviceOreintation == portrait{
//show 4 subviews
}
else{
//show 1 subview and hide rest of the three
}
}
I tried something like this:
func showSubviews(){
if self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClass.Regular {
//show 4 subviews
}
else{
//show 1 subview and hide the rest three
}
}
Any help is appreciated, thanks!
Your code looks correct. You just need to override the viewWillTransitionToSize(_:withTransitionCoordinator:) method in your view controller and do some checking there.
In storyboard, I add 3 views named: red, green, blue with autolayout:
all: fixed width, height, center X
red: top
green: center Y
blue: bottom
add this line in viewDidLoad
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("didChangeOrientation"), name: UIApplicationDidChangeStatusBarOrientationNotification, object: nil)
Add new func:
func didChangeOrientation() {
let orientation = UIApplication.sharedApplication().statusBarOrientation
if UIInterfaceOrientationIsLandscape(orientation) {
green.hidden = true
print("landscape")
}
else {
green.hidden = false
print("portrait")
}
}
Hope this can help.