I noticed that after an orientation change from portrait to landscape, I'm not getting touchesBegan events for some parts of my view any longer. I suppose that this is because I'm not informing my UIView about the dimension change of my window's frame after the device rotation.
I'm setting up everything programmatically (UIWindow, UIViewController, UIView) like this:
myViewController = [[myUIViewController alloc] init];
myWindow = [[UIWindow alloc] initWithFrame: rect];
myView = [[myUIView alloc] initWithFrame: [myWindow bounds]];
[myViewController setView:myView];
[myWindow addSubview:myView];
[myWindow setFrame:rect];
[myWindow makeKeyAndVisible];
When I get the didRotateFromInterfaceOrientation notification, I'm updating the window frame like this:
[[[self view] window] setFrame:rect];
But after that, my UIView does no longer get touchesXXX events for all areas. It seems that only the areas of the previous frame are still reporting events. So my question: Is there anything else I need to do in didRotateFromInterfaceOrientation to inform my UIView about the dimension change?
Thanks for help!
EDIT: Do I have to reposition the UIView on didRotateFromInterfaceOrientation() or is this done automatically? I noticed that the "transform" property of my UIView is set to a transformation matrix when the orientation changes. However, this makes it very hard to reposition my view. The docs say that the "frame" property can't be used when a transformation is active, so I tried to modify the "center" property to reposition my view, but this also doesn't work correctly. I want to move the view to the top-left corner, so I set "center" to (screenwidth/2,screenheight/2) but it doesn't position the view correctly :( Any idea or info what must be done to get the events right in orientation mode?
I have had this same problem, and I believe it is a frame issue.
I had to manually set the rects depending on orientation, as well as set the userInteractionEnabled on the main view, like:
if (appOrientation == 0)
appOrientation = [UIApplication sharedApplication].statusBarOrientation;
if (appOrientation == UIInterfaceOrientationLandscapeLeft || appOrientation == UIInterfaceOrientationLandscapeRight) {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
myView.frame = CGRectMake(0, 0, 1024, 768);
} else {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
myView.frame = CGRectMake(0, 0, 768, 1024);
}
myView.userInteractionEnabled = YES;
OK I know this is an old question but this is how I fixed this issue.
In my situation I had a storyboard with a view that would be displayed either in portrait or forced to landscape mode depending on a user setting.
My app displays the statusBar at all times except for when I'm showing this view.
To make this all work, the transformations had to be applied in the viewWillAppear method for one. I had the following code in the viewDidAppear method at first and that messed with the bounds for the touchesBegan event I guess.
Here's the viewWillAppear code:
- (void)viewWillAppear:(BOOL)animated
{
// Hide the status bar
[[UIApplication sharedApplication] setStatusBarHidden:YES];
// This is to offset the frame so that the view will take the fullscreen once the status bar is hidden
self.navigationController.navigationBar.frame = CGRectOffset(self.navigationController.navigationBar.frame, 0.0, -20.0);
// If view is to be displayed in landscape mode
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"orientation"])
{
// Change status bar orientation
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft];
// Turn the view 90 degrees
[self.navigationController.view setTransform:CGAffineTransformMakeRotation(M_PI/2)];
// Set the frame and the bounds for the view
// Please Note: Frame size has to be reversed for some reason.
[self.navigationController.view setFrame: CGRectMake(0,0,320,480)];
[self.navigationController.view setBounds: CGRectMake(0,0,480,320)];
// Make sure user can interact with view
[self.navigationController.view setUserInteractionEnabled:YES];
}
}
Any other thing that had to happen layout wise, have to happen in the viewDidAppear method. For instance I had an image that covered the whole view and the frame had to be set depending on the orientation. Setting the frame in the viewWillAppear gave weird results but the same code worked perfectly in viewDidAppear.
Hope this helps someone as I banged my head for 6 hours on this thing.
Related
I'm running into a bit of a weird problem when hiding the status bar after the view had loaded. If I add the following method in the ViewDidLoad method, the status bar is completely removed from the view:
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
However, if I call this method in an IBAction or another method, the status bar still slides away but leaves a black bar the same height as itself behind.
I've thought about shifting the entire view up by 20px but is this really a fix? I don't want to just overlap a black bar incase the status bar height changes in future OS upgrades.
Hard-coding any number is always counter to future proofing. Your concerns are correct. There is a bit of a trick to properly handling the hiding of the statusBar. But all the needed information is available.
For example the UIApplication singleton has a property named statusBarFrame which is precisely what it sounds like, a CGRect of the statusBar's frame. The cool thing is that once you have called setStatusBarHidden:withAnimation: that property will give you the new frame, even before the animation completes. So really you are simply left with some basic math to adjust the view's frame.
In short your gut feeling is correct; always compute things live.
I've had good success with a category method like this. (Even when toggling in-call status bar in simulator(Command - T)):
#implementation UIApplication (nj_SmartStatusBar)
// Always designate your custom methods by prefix.
-(void)nj_setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation{
UIWindow *window = [self.windows objectAtIndex:0];
UIViewController *rootViewController = window.rootViewController;
UIView *view = rootViewController.view;
// slight optimization to avoid unnecassary calls.
BOOL isHiddenNow = self.statusBarHidden;
if (hidden == isHiddenNow) return;
// Hide/Unhide the status bar
[self setStatusBarHidden:hidden withAnimation:animation];
// Get statusBar's frame
CGRect statusBarFrame = self.statusBarFrame;
// Establish a baseline frame.
CGRect newViewFrame = window.bounds;
// Check if statusBar's frame is worth dodging.
if (!CGRectEqualToRect(statusBarFrame, CGRectZero)){
UIInterfaceOrientation currentOrientation = rootViewController.interfaceOrientation;
if (UIInterfaceOrientationIsPortrait(currentOrientation)){
// If portrait we need to shrink height
newViewFrame.size.height -= statusBarFrame.size.height;
if (currentOrientation == UIInterfaceOrientationPortrait){
// If not upside-down move down the origin.
newViewFrame.origin.y += statusBarFrame.size.height;
}
} else { // Is landscape / Slightly trickier.
// For portrait we shink width (for status bar on side of window)
newViewFrame.size.width -= statusBarFrame.size.width;
if (currentOrientation == UIInterfaceOrientationLandscapeLeft){
// If the status bar is on the left side of the window we move the origin over.
newViewFrame.origin.x += statusBarFrame.size.width;
}
}
}
// Animate... Play with duration later...
[UIView animateWithDuration:0.35 animations:^{
view.frame = newViewFrame;
}];
}
#end
Why are you calling this in viewDidLoad?
Try it in loadView?
- (void)loadView {
[super loadView];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}
Yes, moving the view 20 pixels should fix your problem. The black is absence of anything to display, not an actual black bar.
As for potential status height changes, this fix will not work if that happens because the view will be moved by the height of the new status bar. If that happens, you will have to either add different offsets for different status bars, or find a completely new solution.
In viewWillAppear:
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
In viewDidAppear, you can insert:
self.view.window.rootViewController.view.frame = [UIScreen mainScreen].applicationFrame;
In viewWillDisappear:
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
I was able to fix this issue by calling:
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
as others recommended before presenting the view controller that is displaying the black bar.
For example, if I had an action that presented ViewController, I would call it like so:
- (IBAction)presentViewController:(id)sender {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil];
}
I have an application like the photos app where the main view is a UIScrollView which takes up the full size of the screen. Also, like the photos app, when the user taps the screen there are translucent navigation, status, and tool bars which reappear / disappear.
I am having a problem setting the UIViewControllers main view as a UIScrollView and having it take up the full length of the screen. The problem is that when the navigation and status bars are shown, the UIScrollView gets pushed down by the height of the navigation and status bars (it doesn't go underneath them like it's suppose to). When the user taps the screen and the navigation / status bars disappear, then it resets itself to take up the full length of the screen like it's suppose to.
A simple work around of setting the main view as a UIView and attaching a UIScrollView on top of it works. However, I'd like to try and get this to work without any workarounds (ie adjusting the UIScrollViews contentInset, etc) because in theory it should work.
Below is the code I'm implementing:
- (void)loadView
{
self.wantsFullScreenLayout = YES;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake(0,0,320,480)];
scrollView.contentSize = CGSizeMake(320, 480);
scrollView.scrollEnabled = NO;
scrollView.contentOffset = CGPointZero;
scrollView.bounces = NO;
self.view = scrollView;
[scrollView release];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBar.translucent = YES;
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackTranslucent animated: NO];
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbar.barStyle = UIBarStyleBlack;
self.navigationController.toolbar.translucent = YES;
[self startTimer];
}
- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationController.navigationBar.translucent = NO;
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleDefault animated: NO];
[self cancelTimer];
}
UPDATE: I've noticed it's the contentOffset and contentInset that are changing, not the scrollViews frame. When the bars have disappeared and the UIScrollView is the full size of the screen (as it should be), the contentOffset and contentInset are as follows:
Content Offset: {0, -20}
Content Inset: {20, 0, 44, 0}
When the bars are visible and the UIScrollView is pushed down, the contentOffset and contentInset are as follows:
Content Offset: {0, -64}
Content Inset: {64, 0, 44, 0}
I solved a similar issue on iOS7 after reading Apple's UI Transition Guide:
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/AppearanceCustomization.html
It turns out UIViewController has a automaticallyAdjustsScrollViewInsets boolean property.
Default is true, disabling this made my UIScrollView fill up the entire window, instead of being pushed down.
I spoke with DTS about this and they said this is the designed behavior and recommended to attach the UIScrollView to a parent UIView.
Try be setting:
self.view.frame = CGRectMake(0.f, -44.f, 320.f 480.f);
It may not size as you wish, but you can adjust it...
What is the safest and most effective way place a new UIView within the bounds of the App's UIWindow on the top of the view stack when a screen rotation from Portrait to Landscape occurs? Also, when returning from Landscape to Portrait, to remove this subview.
Basically the App is created as most are:
-UIWindow:
--UIView
---All subviews (including a tabview controller)
I would like to create:
-UIWindow:
--UIView (new, to be placed on top)
--UIView
---All subviews (and triggered by a view controller in here)
Is this wrong? There is surprisingly little documentation to help do this.
If you create a view controller to hold all of your subviews you can just use the rotation functions that will be called for you:
willRotateToInterfaceOrientation:duration:
and
didRotateFromInterfaceOrientation:
So lets say you use didRotateFromInterfaceOrientation you check
if(UIInterfaceOrientationIsLandscape(orientation))
{
[yourView removeFromSuperView];
}
else
{
[self.view addSubView: yourView];
}
See my answer here: https://stackoverflow.com/a/4960988/202451
It should bring you closer to doing custom things like that
I found the a working solution. Might you offer a better one?
ArtsDayAppDelegate *appDelegate = (ArtsDayAppDelegate*)[[UIApplication sharedApplication] delegate];
UIView *landscapeView;
if (!landscapeView) {
landscapeView = [[UIView alloc] initWithFrame: frame];
}
[landscapeView setBackgroundColor:[UIColor blackColor]];
..add various views..
[appDelegate.window.rootViewController.view addSubview:landscapeView];
I started by creating a universal window based app. Starting with the iPhone version I created a UIViewController and associated nib.
My App delegate:
rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
[window makeKeyAndVisible];
[window addSubview:rootViewController.view];
return YES;
My RootViewController:
- (void)viewDidLoad {
[super viewDidLoad];
adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero()];
[self.view addSubview:adBannerView];
}
I've tried instanciating buttons instead of the adBanner and I get the same result.
My RootViewController's nib has not been changed since x-code created it for me.
My MainWindow_iPhone.xib also is stock.
What's causing this?
Update
After changing the app's orientation the adBannerView (or button...) will snap into the correct place at y=0. I've tried setting adBannerView's y location to 20 presumably to compensate for the status bar and that makes everything display correctly until I change orientation. Then everything moves down 20 pixels and will leave a 20 pixel space between the adBannerView and the status bar.
Try to add the next line in your viewDidLoad (right after [super viewDidLoad];):
self.view.frame = [[UIScreen mainScreen] applicationFrame];
CGRectZero is literally a zero rect (0, 0, 0, 0), so ADBannerView should never show up if it really has a width and height of 0. You probably want to try initWithFrame:self.view.frame or so…
You should set the size identifier before adding the view:
- (void)viewDidLoad {
[super viewDidLoad];
adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero()];
if(UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
else
adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;
[self.view addSubview:adBannerView];
// now you can treat it like any other subview
// For example, if you want to move it to the bottom of the view, do this:
CGRect frame = adBannerView.frame;
frame.origin.y = self.view.frame.size.height - frame.size.height;
[adBannerView setFrame:frame];
}
Whenever the interface rotates, you should notify the banner to change its size.
Assuming you have access to WWDC videos (which is available for free), check video session 305. It demos adding the banner.
I have a UIView and a UIController view. My is standard a 320x460 view. In applicationDidFinishLaunching I do:
[window addSubview:[controller view]];
The weird thing is that the UIView goes under the status bar (like there's missing outlet). However, if I rotate iPhone to the side and then back, it shows up ok.
Is this an expected behavior (I bet I can fix it by setting offset) or am I doing smth wrong?
I ran into this issue when displaying a UIViewController via presentModalViewController.
You can get around it by manually resizing the controller's view after the view has appeared:
- (void) viewDidAppear: (BOOL) animated {
//manually adjust the frame of the main view to prevent it from appearing under the status bar.
UIApplication *app = [UIApplication sharedApplication];
if(!app.statusBarHidden) {
[self.view setFrame:CGRectMake(0.0,app.statusBarFrame.size.height, self.view.bounds.size.width, self.view.bounds.size.height - app.statusBarFrame.size.height)];
}
}
I think your problem is that when you add a view to a window, you need to be aware of the state of the status bar and compensate for it:
if showing the status bar :
[controller view].frame = CGRectMake(0, **20**, 320, 460);
else
[controller view].frame = CGRectMake(0, 0, 320, **480**);
this is why IB shows you a dummy status bar.
I add this issue today. It turned out that I had "Wants Full Screen" checked in the ViewController's Attribute inspector.
Turning off "Wants Full Screen" resolved the problem.
Finally, I got to this solution. Works well for both iPhone & iPad:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Allocate view controller and load nib file
if (isIPhone) {
self.mainViewController = [[[tfdMainViewController_iPhone alloc] initWithNibName:#"tfdMainViewController_iPhone" bundle:nil] autorelease];
} else {
self.mainViewController = [[[tfdMainViewController_iPad alloc] initWithNibName:#"tfdMainViewController_iPad" bundle:nil] autorelease];
}
// Offset correction (iPhone bug?)
CGRect r = self.mainViewController.view.frame;
r = CGRectOffset(r, 0, [UIApplication sharedApplication].statusBarFrame.size.height);
[self.mainViewController.view setFrame:r];
[window addSubview: self.mainViewController.view];
[self.window makeKeyAndVisible];
return YES;
}
P.S. For some reason view has correct height:
480 (screen height in iPhone Portrait mode) - 20 (status bar) = 460,
but failed to set vertical offset. It is pretty strange behavior, looks like bug.
Fixes to the window didn't work for me as I had a modal view. A UIModalPresentationCurrentContext modal view. OK, here's what worked for me. I've been searching the web up and down before getting this to work.
I'm unable to move the view.frame from the parent. However in the viewWillAppear I'm able to move it down:
- (void)viewWillAppear:(BOOL)animated
{
// Move down to compensate for statusbar
CGRect frame = parentView.navCon.view.frame;
frame.origin.y = [UIApplication sharedApplication].statusBarFrame.size.height;
parentView.navCon.view.frame = frame;
}
If you are using Interface Builder's "Simulated User Interface Elements", then you also need to make sure that you have set the flag for "Resize View From NIB" in your MainWindow nib.
This appears to be a bug in iOS 5. One fix would be to use wantsFullScreenLayout in whatever view controller needs to present modally and manually layout the view always below the status bar.
http://www.cocoanetics.com/2012/06/radar-view-frame-inconsistency-using-presentviewcontroller-wantsfullscreenlayout-yn/
http://openradar.appspot.com/radar?id=1758406
Are you manually setting the application bar hidden property AFTER adding the subview? I don't imagine this is the case, but if it's set to none when you first load the view it will layout as if there isn't one, and if you then set the status bar to not hidden it will pop up on top of your view.
A possible solution is to use [[controller view] setNeedsLayout]; after adding the subview, or possibly [window layoutSubviews];. I've never had a lot of success using those to fix layout problems, but since it works after a rotation it's worth a shot.
Even me too got the same issue. When we are using some coding for device orientation we have wrote some coding in app delegate or in our view controller. There we need to change the condition to use the orientation return YES or NO. That solved our issue.
I prefer to use UINavigationController to wrap the UIViewController you, after that, set the NavigationBarHidden to the UINavigationController. It's perfect solution cause UINavigationController do handle the height of status bar in iOS 7.
Here is my code and my screen capture.
UINavigationController *wrapNavController = [[UINavigationController alloc] initWithRootViewController:yourViewController] ;
[wrapNavController setNavigationBarHidden:YES] ;
The Xcode 6, iOS7/8 solution is in to uncheck the "Under Top Bars" checkmark in the "Extend Edges" section of the View Controller section of the Attributes Inspector.
For Xamarin.iOS it would be:
if (UIApplication.SharedApplication.StatusBarHidden == false)
{
var statusBarHeight = UIApplication.SharedApplication.StatusBarFrame.Height;
View.Frame = new CoreGraphics.CGRect(0, statusBarHeight, View.Frame.Width, View.Frame.Height - statusBarHeight);
}