i am working compatablity modification for ios 7. My application view is rotating for landscape and portiate mode.
But currently in ios 7 status bar is overlayping to my view.in all rotation. I have try with following code but it not work for me.
In my appDelegate file appdidfinishlaunching i set 1 notification as follows:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidChangeStatusBarOrientation:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
}
This will cold method as follows:
- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
{
int a = [[notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey] intValue];
int w = [[UIScreen mainScreen] bounds].size.width;
int h = [[UIScreen mainScreen] bounds].size.height;
switch(a){
case 4:
self.window.frame = CGRectMake(0,20,w,h);
break;
case 3:
self.window.frame = CGRectMake(-20,0,w-20,h+20);
break;
case 2:
self.window.frame = CGRectMake(0,-20,w,h);
break;
case 1:
self.window.frame = CGRectMake(20,0,w-20,h+20);
}
}
For the first time when i launch my application status bar was over layping my view.
Secound time when i rotate my iPad at that time it set status bar black ground on the different possition.
Every time I was getting wrong.
Please help me, Am i doing anything wrong in the above.
And if possible please provide me sample code for all rotation to stop status bar overlayping.
You can stop the navigation bar from overlapping by using this code
self.navigationController.navigationBar.translucent = NO;
Related
Is there a way to detect that your iPhone app us running 2x/1x on an iPad?
I need to be able to detect the difference in points per inch for my app.
Check the scale property:
[[UIScreen mainScreen] scale]
Here's a handy function:
+(BOOL) screenIs2xResolution {
return 2.0 == [MyDeviceClass mainScreenScale];
}
+(CGFloat) mainScreenScale {
CGFloat scale = 1.0;
UIScreen* screen = [UIScreen mainScreen];
if ([UIScreen instancesRespondToSelector:#selector(scale)]) {
scale = [screen scale];
}
return scale;
}
Credits: http://www.markj.net/iphone-4-2x-graphics-scale-ipad/
See also: http://struct.ca/2010/high-res-graphics-in-cocos2d/
Since you cannot register for _UIClassicApplicationWillChangeZoomNotificationName, it seems to be an internal constant, what I did is:
Register for any Notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(changeZoom:) name:nil object:nil];
And then check the according values:
- (void)changeZoom:(NSNotification*)notification
{
if ([[notification name] isEqualToString:#"_UIClassicApplicationWillChangeZoomNotificationName"])
{
NSLog(#"Zoom changed to %#", [[[notification userInfo] objectForKey:#"_UIClassicIsZoomedUserInfoKeyName"] boolValue] == 0 ? #"1x" : #"2x");
}
}
You can detect the change by registering for the notification _UIClassicApplicationWillChangeZoomNotificationName and then handle the scale change using more or less the method outlined by #magma in his answer. _UIClassicApplicationWillChangeZoomNotificationName will tell you when the "2x"/"1x" button has been used to change the scale.
EDIT: The given answer works on the device, but beware it fails on the simulator.
When my iPad starts up, I show a loading label, centered in the middle of the screen. I set its autoresizingMask so it recenters on orientation change.
As the app starts up, the label's text changes, so I want to recenter the label based on its new length. However, the following piece of code doesn't center the label correctly:
- (void) setLabelText:(NSString*)text {
CGSize maximumLabelSize = CGSizeMake(500,20);
CGSize expectedLabelSize = [text sizeWithFont:loadingLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:loadingLabel.lineBreakMode];
loadingLabel.frame = CGRectMake(self.view.frame.size.width/2-expectedLabelSize.width/2,
loadingLabel.frame.origin.y,
expectedLabelSize.width,
loadingLabel.frame.size.height);
loadingLabel.text = text;
}
I also considered checking [[UIDevice currentDevice]orientation], and if the iPad is in landscape mode, then I'd use self.view.frame.size.height to set the xOrigin of the label.
However, if the device is face up or face down, (and not landscape or portrait) then this method fails. I also have a lastOrientation variable in my appDelegate, which remembers if the app is in landscape or portrait, even when face up or face down, based on the device's last known orientation. However, at start-up, this variable isn't necessarily set.
Is there some simple solution I am missing here, so I can resize and center my label?
EDIT: I tried checking UIStatusBarOrientation based on the advice posted, but it doesn't work:
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft
|| [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
NSLog(#"landscape");
width = self.view.frame.size.height;
} else {
NSLog(#"portrait");
}
This always logs portrait, at least on start-up, on the simulator.
Check [[UIApplication sharedApplication] statusBarOrientation]
I found a trick to solve the FaceUp orientation issue!!!
Delay the orientation check till AFTER the app has started running, then set variables, view sizes, etc.!!!
//CODE
- (void)viewDidLoad {
[super viewDidLoad];
//DELAY
[NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:#selector(delayedCheck)
userInfo:nil
repeats:NO];
}
-(void)delayedCheck{
//DETERMINE ORIENTATION
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ){
FACING = #"PU";
}
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown ){
FACING = #"PD";
}
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){
FACING = #"LL";
}
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight ){
FACING = #"LR";
}
//DETERMINE ORIENTATION
//START
[self setStuff];
//START
}
-(void)setStuff{
if( FACING == #"PU" ){
//logic for Portrait
}
else
if( FACING == #"PD" ){
//logic for PortraitUpsideDown
}
else{
if( FACING == #"LL"){
//logic for LandscapeLeft
}
else
if( FACING == #"LR" ){
//logic for LandscapeRight
}
}
//CODE
You can addSubviews, position elements, etc. in the 'setStuff' function ... anything that would initially depend on the orientation!!!
:D
-Chris Allinson
I am having a problem trying to get the orientation of the device correct. I have to show an action sheet that should come depending on the orientation of the device. Here is the code I am using.
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
switch (orientation)
{
case UIDeviceOrientationPortrait:
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationFaceDown:
case UIDeviceOrientationUnknown:
case UIDeviceOrientationPortraitUpsideDown:
[self displayActionSheetInPotraitMode];
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
[self displayActionSheetInLandscapeMode];
break;
default:
[self displayActionSheetInPotraitMode];
break;
}
Let's look what the UIDevice Class Reference tells us about the orientation property.
The value of this property always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications.
So, you should call
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]
somewhere earlier in your code.
Also, I'd like to suggest a way to simplify your current code:
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(orientation)) {
[self displayActionSheetInLandscapeMode];
} else {
[self displayActionSheetInPotraitMode];
}
See the answer here: UIActionSheet orientation
You should add a UIView (add it as subview to UIWindow) -> transform it to proper orientation and then add the UIActionSheet to this UIView.
In my program I'm moving things based on rotation, but I'm not rotating the entire view. I'm Using :
static UIDeviceOrientation previousOrientation = UIDeviceOrientationPortrait;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (void) didRotate:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[self doRotationStuff:orientation: previousOrientation];
previousOrientation = orientation;
}
This works as long as, when the program is launched, the device orientation is portrait, but not if the initial orientation is landscape or upside down, because [self doRotationStuff] makes changes relative to the difference from the previous orientation.
Is there a way to detect the orientation either at launch, or right before the device is rotated?
Depending on your circumstances, a simpler option may be the interfaceOrientation property of the UIViewController class. This is correct before a rotation.
Updated:
So, from the comment discussion, it appears that you can't rely on [UIDevice currentDevice].orientation until the orientation actually changes for the first time. If so, you could probably hack it by getting raw accelerometer readings.
#define kUpdateFrequency 30 // Hz
#define kUpdateCount 15 // So we init after half a second
#define kFilteringFactor (1.0f / kUpdateCount)
- (void)applicationDidFinishLaunching:(UIApplication *)app
{
[UIAccelerometer sharedAccelerometer].updateInterval = (1.0 / kUpdateFrequency);
[UIAccelerometer sharedAccelerometer].delegate = self;
accelerometerCounter = 0;
...
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)accel
{
// Average out the first kUpdateCount readings
// acceleration_[xyz] are ivars typed float
acceleration_x = (float)accel.x * kFilteringFactor + acceleration_x * (1.0f - kFilteringFactor);
acceleration_y = (float)accel.y * kFilteringFactor + acceleration_y * (1.0f - kFilteringFactor);
acceleration_z = (float)accel.z * kFilteringFactor + acceleration_z * (1.0f - kFilteringFactor);
accelerometerCounter++;
if (accelerometerCounter == kUpdateCount)
{
[self initOrientation];
[UIAccelerometer sharedAccelerometer].delegate = nil;
}
}
- (void)initOrientation
{
// Figure out orientation from acceleration_[xyz] and set up your UI...
}
Original response:
Does [UIDevice currentDevice].orientation return the correct orientation during applicationDidFinishLaunching:? If so, you can set up your initial UI according to that orientation.
If that property doesn't get set until some later time, you might try experimenting with performSelector:afterDelay: to initialize the UI after a small delay.
This code sample is from Kendall's answer below, added here for completeness:
[self performSelector:#selector(getOriented) withObject:nil afterDelay:0.0f];
I'm not sure if a zero-second delay is sufficient -- this means the code for getOriented will run during the first pass through the event run loop. You may need to wait longer for the accelerometer readings to register on UIDevice.
Mort, these answers seem somewhat of a red herring; I can't see why you can't use the following built-in method for a UIViewController class:
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {}
This method gets called automatically after a rotation has occurred (rather than with shouldAutorotateToInterfaceOrientation which only tells you it's about to happen). Handily, the variable 'fromInterfaceOrientation' contains the previous orientation. As the documentation also says, you can assume that the interfaceOrientation property of the view has already been set to the new orientation, so you then have one method with access to the old orientation and the new!
If I've missed something and you've already dismissed being able to use this method, my apologies! It just seems odd that you're creating and storing a variable for the "previous orientation" when it's provided for free in the above method.
Hope that helps!
Use this for the orientation of the UI if you need to determine what way are you pointing.
Not 100% sure this is right but going off the top of my head:
[[[UIApplication sharedApplication] statusBar] orientation]
Here's one way to get the orientation when the app first loads and the UIDeviceOrientation is set to UIDeviceOrientationUnknown. You can look at the transform property of the rotated view.
if(toInterface == UIDeviceOrientationUnknown) {
CGAffineTransform trans = navigationController.view.transform;
if(trans.b == 1 && trans.c == -1)
toInterface = UIDeviceOrientationLandscapeLeft;
else if(trans.b == -1 && trans.c == 1)
toInterface = UIDeviceOrientationLandscapeRight;
else if(trans.a == -1 && trans.d == -1)
toInterface = UIDeviceOrientationPortraitUpsideDown;
else
toInterface = UIDeviceOrientationPortrait;
}
A more complete example on how to obtain device orientation from accelerator readings can be found here
As the solution relies on accelerator readings, it wouldn't work on the simulator, so you'll have to work on the device... still looking myself for a solution that works on the simulator.
In response to your comment, I thought I could better put code here than in a comment (though really Daniel deserves credit here):
in applicationDidFinishLaunching:
[self performSelector:#selector(getOriented) withObject:nil afterDelay:0.0f];
Then you just need the method to call:
- (void) getOriented
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
// save orientation somewhere
}
Im having a modal view displayed on top of tab bar controller the orientation of tab bar controller and my modal view is currently in portrait mode, i need to only change my modal views orientation to landscape mode,
i Tried usign UIDevice Orientation but no success it only works when .plist has UIDeviceOrientationKey, and i dont want whole of my application in landscape mode. secondly i even tried using UIApplication setStatusBarOrientation but my view doesn't respond to it i.e. only my device(simulator) get rotated but the view still remains in portrait mode, using affine transformation on UIView creates an empty view my view is created through XIB.
Please Help
Thanks Rakesh
I have used this and it works well with some tweaking.
Until today I used the following solution:
-(void)setOrientation:(UIInterfaceOrientation)orientation
{
SEL rotation = #selector(_setRotatableViewOrientation:duration:);
NSMethodSignature * methodSignature = [window methodSignatureForSelector:rotation];
if (methodSignature)
{
NSInvocation * ivc = [NSInvocation invocationWithMethodSignature:methodSignature];
[ivc setTarget:window];
[ivc setSelector:rotation];
// arg0 will be self, 1 will be cmd_ (current method selector)
[ivc setArgument:&orientation atIndex:2];
double duration = 0.4;
[ivc setArgument:&duration atIndex:3];
[ivc invoke];
}
}
-(void)normalizeOrientation
{
UIDeviceOrientation dOrientation = [UIDevice currentDevice].orientation;
int orientation = -1;
switch(dOrientation)
{
case UIDeviceOrientationPortrait:
orientation = UIInterfaceOrientationPortrait;
break;
case UIDeviceOrientationPortraitUpsideDown:
orientation = UIInterfaceOrientationPortraitUpsideDown;
break;
case UIDeviceOrientationLandscapeRight:
orientation = UIInterfaceOrientationLandscapeLeft;
break;
case UIDeviceOrientationLandscapeLeft:
orientation = UIInterfaceOrientationLandscapeRight;
break;
}
if (orientation != -1 && orientation != tabBarController.interfaceOrientation)
[self setOrientation:orientation];
}
but the official AppStore wont accept new releases of my App due to
_setRotatableViewOrientation:duration:transition:toView:
isn't documented API.
So be aware for use it in your commercial projects!
Apple new automated API checker will reject your App!