It works perfect fine when starting in portrait and also works when you rotate from portrait to landscape and back.
It does not work when starting in landscape. But then it works when you rotate from landscape to portrait and back.
In landscape starting mode, the screen does not respond with any touch where screen coordinateX greater than 768.
What happens in code is, I use status bar orientation to determine original orientation and rotate each view manually. The views display correctly but does not receive touch properly.
Then my root view controller will get called when ipad start rotating with:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
which will rotate every subviews.
Root controller:
- (void)loadView {
self.view = [[UIView alloc]init ];
//initialize child views
[self willRotateToInterfaceOrientation:0 duration:0];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if ([model isLandscape]) {
self.view.frame = CGRectMake(0, 0, 1024, 768-80);
}
else {
self.view.frame = CGRectMake(0, 0, 768, 1024-80);
}
//rotate child views
}
My code [model isLandscape] works so I don't need to provide details as to how it works but here are the code anyway:
- (bool)isLandscape {
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
return true;
else
return false;
}
-(id) init
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIInterfaceOrientation curOrientation = [[UIDevice currentDevice] orientation];
if (curOrientation == UIDeviceOrientationPortrait ||
curOrientation == UIDeviceOrientationPortraitUpsideDown ||
curOrientation == UIDeviceOrientationLandscapeLeft ||
curOrientation == UIDeviceOrientationLandscapeRight)
{
orientation = curOrientation;
((AppDelegate*)([UIApplication sharedApplication].delegate)).savedOrientationForRestart = orientation;
NSLog(#"changed");
}
}
-(void)validateOrientation { //first time when initializing orientation
UIInterfaceOrientation curOrientation = [[UIDevice currentDevice] orientation];
if (curOrientation != UIDeviceOrientationPortrait &&
curOrientation != UIDeviceOrientationPortraitUpsideDown &&
curOrientation != UIDeviceOrientationLandscapeLeft &&
curOrientation != UIDeviceOrientationLandscapeRight)
{
orientation = [[UIApplication sharedApplication] statusBarOrientation];
}
}
OK. so it turns out among the tens of backgrounds and layers some were not rotated to landscape/portrait correctly...it was extremely hard to debug this issue...
in the end, I hooked up a touch debugger to see which views are receiving touch correctly and which are not...
extremely helpful!
Just use the UIWindow subclass below instead of UIWindow in your code, should work.
#import <UIKit/UIKit.h>
#interface MyUIWindow : UIWindow {
}
-(void)rotate;
#end
and
#import "MyUIWindow.h"
#implementation MyUIWindow
-(id) init{
self = [super init];
if(self)
{
}
return self;
}
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
NSSet *touches = [event allTouches];
if (touches.count != 1)
return;
UITouch *touch = touches.anyObject;
UIView * view = touch.view;
NSLog(NSStringFromClass([touch.view class])); //this prints out touch view
}
#end
Related
everybody.
Guys, I have a problem with status bar in my project..
In default , in app status bar should hidden. This is work, but one view have a bug with status bar like on screenshot.
On main menu it's have black color and don't hidden(
In singleton I'm initialise background image for different devices, and this is work for all views in app))
This is my code singleton init background:
// background return
- (UIColor *)mainBackground:(UIInterfaceOrientation)orientation {
UIColor *color;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
color = [UIColor colorWithPatternImage:[UIImage imageNamed:#"backgroundIpad.png"]];
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
color = [UIColor colorWithPatternImage:[UIImage imageNamed:#"backgroundLandspIpad.png"]];
}
} else {
color = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
}
return color;
}
//return wight screen
- (float)wightScreen {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
return 768;
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
return 1024;
}
}
return wight();
}
//return rect view all for screens
- (CGRect)refreshPoints {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
return CGRectMake(0, 0, 768, 1024);
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
return CGRectMake(0, 0, 1024, 768);
}
} else {
return CGRectMake(0, 0, 320, height());
}
return CGRectMake(0, 0, 768, 1024);
}
And this is code in controller view, where I'm have a problem:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
return [[Singleton sharedInstance] getSelf:#"MainMenuController"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[PostRequest sharedInstance] userInfo];
self.view.backgroundColor = [[Singleton sharedInstance] mainBackground:[[UIApplication sharedApplication] statusBarOrientation]];
// if (iphone4()) {
// [self newRectNiewIphone];
// }
}
I'm hope in yours help!
This is screenshot with bug status bar
http://i.stack.imgur.com/nfiZ3.png
And screenshot, how on another views
http://i.stack.imgur.com/wVadC.png
You need to set
self.automaticallyAdjustsScrollViewInsets=NO;
On the viewController in the viewDidLoad method.
I have seen multiple copies of the same question.but it has not supported my cause.
i have tried subclassing the navigationController adding category for the navigation controller and when i tried all these the methods shouldAutoRotate and supportedInterfaceOrientations gets invoked for Both the navigationcontroller and view controller but the device stays in portrait view only.
#import "UINavigationController+Rotation.h"
#implementation UINavigationController (Rotation)
- (BOOL)shouldAutorotate {
BOOL result = self.topViewController.shouldAutorotate;
return result;
}
- (NSUInteger)supportedInterfaceOrientations {
NSUInteger result = self.topViewController.supportedInterfaceOrientations;
return result;
}
#end
Is some thing that i do wrong?
Call this method form viewdidload:
-(void)rotateCameraView {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(deviceOrientationDidChange:) name:
UIDeviceOrientationDidChangeNotification object: nil];
}
When you rotate device this method will call automatically
-(void)deviceOrientationDidChange:(NSNotification *)notification {
//Obtaining the current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//Ignoring specific orientations
if (orientation == UIDeviceOrientationFaceUp
|| orientation == UIDeviceOrientationFaceDown || orientation ==
UIDeviceOrientationUnknown) { return; }
if ((UIDeviceOrientationIsPortrait(orientation)
||UIDeviceOrientationIsPortrait(orientation)) ||
(UIDeviceOrientationIsLandscape(orientation) ||
UIDeviceOrientationIsLandscape(orientation))) {
//still saving the current orientation
currentOrientation = orientation; } [self
performSelector:#selector(orientationChangedMethod) withObject:nil
afterDelay:0];
}
-(void)orientationChangedMethod
{
switch (currentOrientation) {
case UIDeviceOrientationPortrait:
if (isOrientationEffect==YES)
{
appDelegate.imagePickerController.cameraViewTransform = CGAffineTransformMakeRotation(M_PI);
// if (!isPurchased)
// {
//
// [self buttonFrameWithIad];
// }
// else
// {
// [self buttonFrameWithoutIad];
// }
[self MakeTransformRotation];
}
[UIView beginAnimations:#"rotateView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.35f];
camDefaultImg.transform = CGAffineTransformMakeRotation(0);
[UIView commitAnimations];
break;
case UIDeviceOrientationLandscapeLeft:
if (isOrientationEffect==YES)
{
}
}
I have an app that changes the view when the orientation changes using: [[UIApplication sharedApplication] statusBarOrientation]. On the iPhone, it works but on the iPad, it gives me landscape instead of portrait and portrait instead of landscape.
When I call the getRelevantFrame method outside of the UIDeviceOrientationDidChangeNotification, it will return the correct frame.
It will return the opposite frame (if landscape, will return portrait and vice versa) when responding to the notification.
Both the versions (iPod/iPhone + iPad) uses the same code but this only breaks on the iPad version
Here is the code I used to calculate the relevant frame:
EDIT: Used rob5408's advice. Changed to use UI_USER_INTERFACE_IDIOM().
+ (CGRect)getRelevantFrame {
//Get orientation
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { //iPhone/iPod
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
return [self frameiPhoneLandscape];
}
else {
return [self frameiPhonePortrait];
}
} else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { //iPad
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(#"iPad Landscape");
return [self frameiPadLandscape];
}
else {
NSLog(#"iPad Portrait");
return [self frameiPadPortrait];
}
}
return CGRectMake(0, 0, 0, 0);
}
Here is the notification I used:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
Here is the code I used to read the orientation change:
- (void) didRotate:(NSNotification *)notification
{
NSLog(#"Parent: %#", self.parentController);
if(self.parentController) {
[self setFrame:[TASFrames getRelevantFrame]];
}
}
I transferred the code to a new project. And now it works as it's supposed to. No idea as to why this happens. Code didn't change.
I am using tableview with search bar display controller, it shows good in portait and landscape view, but rotate from landscape to portrait search bar size is reduced, how to change the original size
The first your should catch the orientation:
//AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (nonatomic, unsafe_unretained) NSInteger isPortrait;
//AppDelegate.m
#synthesize isPortrait;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
isPortrait = 1;
}
Implementation state:
-(void)viewWillAppear:(BOOL)animated{
[[UIApplication sharedApplication] statusBarOrientation];
[[UIDevice currentDevice] orientation];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
//methode deviceRotated
-(void)deviceRotated:(NSNotification*)notification{
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
[self searchBarLandscape];
app.isPortrait = NO;
}
else{
[self searchBarPortrait];
app.isPortrait = YES;
}
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
app.isPortrait = 1;
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
app.isPortrait = 0;
}
[self prepareSearchBarOrientation];
}
-(void)prepareSearchBarOrientation
{
AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (app.isPortrait)
{
[self searchBarPortrait];//Set Frame here
}
else
{
[self searchBarLandscape];
}
}
Did you try setting the searchBar's frame in the rotation delegate methods or Try auto resizing in XIB.
See this for a better idea.
Im currently working with a IPhone-app that has a "timetable".
in portrait i want it to have a regular table-view with some customization! When i have the IPhone in landscape i want it to change to a more "timetable"-view, with tables and rows.
Is it possible?
Try This
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self adjustViewsForOrientation:toInterfaceOrientation];
}
-(void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//write code for portrait mode
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//write code for landscape mode
}
}
See Apples documentation Creating an Alternate Landscape Interface
Make sure to read the documentation, but here is the code from the example implementation:
#implementation PortraitViewController
- (void)awakeFromNib
{
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
[self performSegueWithIdentifier:#"DisplayAlternateView" sender:self];
isShowingLandscapeView = YES;
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
isShowingLandscapeView)
{
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
}
}
Answer is Yes it's possible through the following method:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
\\ your timetable customisation goes here
}
}
You also need this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}