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];
Related
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.
Apple's tab bar controller has a lot of limitations. One important limitation is that you can't modify the tab bar in a rejection safe mode. My tab bar has a simple sliding movements and it's multi row.
For those reasons I decided to build a TBVC from the beginning; everything seems to work correctly, but I'm really messing around with rotation. Every time that I change orientation main view frames are changed.
Here is my hierarchy from top to the container view:
-MainView--contains-->TabBarView+containerView
The containerView is the view used to contain views loaded from the other controllers.
Here is the -loadView method of my CustomTabBaViewController
- (void)loadView
{
UIView *theView=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
theView.autoresizingMask=UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
theView.backgroundColor=[UIColor greenColor];
containerOfControllersView=[[UIView alloc] initWithFrame:theView.bounds];
containerOfControllersView.backgroundColor=[UIColor blueColor];
containerOfControllersView.autoresizingMask=UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[theView addSubview:containerOfControllersView];
ideoloTabBar=[[IdeoloTabBar alloc]initWithNumberOfControllers:[controllers count]];
[theView addSubview:ideoloTabBar];
self.view=theView;
[theView release];
}
When I set a new view from another controller I use this method:
-(void)setCurrentViewWithView:(UIView*)theView{
if ([[self.containerOfControllersView subviews] count]>0) {
UIView *tagView=[self.containerOfControllersView viewWithTag:555];
tagView.tag=0;
[tagView removeFromSuperview];
}
theView.tag=555;
theView.autoresizingMask=UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
theView.frame=[[UIScreen mainScreen]applicationFrame];
[self.containerOfControllersView addSubview:theView];
[self.view bringSubviewToFront:ideoloTabBar];
}
As you can see the views from other view controllers are applied using the applicationFrame.
When I rotate the device happens something wrong, the mainview not only is resized according to the new orientation but also moved by 20px (status bar size) to the botton, thus leaving a gap between the status bar and the container view. Since I gave the mainview the screen bounds I can't understand with it should be moved.
UPDATE
I'm trying a different approach so I've modified the loadView like that:
- (void)loadView
{
[super loadView];
containerOfControllersView=[[UIView alloc] initWithFrame:self.view.bounds];
containerOfControllersView.backgroundColor=[UIColor blueColor];
containerOfControllersView.autoresizingMask=UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view.backgroundColor=[UIColor greenColor];
[self.view addSubview:containerOfControllersView];
ideoloTabBar=[[IdeoloTabBar alloc]initWithNumberOfControllers:[controllers count]];
[self.view addSubview:ideoloTabBar];
}
And in the setCurrentViewWithView:(UIView*)theView I've modified the line with
theView.frame=self.view.bounds;
instead of using the applicationFrame.
NOW:
On iPhone when I try to load a modalView it cuts about 40px at the bottom
On iPad when I try to load a modalView it lefts 20px at the bottom, because 20px are under the status bar but wantsFullScreen is NO.
UPDATE 2
It seems that the presentModalViewController should be called from the root view controller. I will create a protocol and an abstract UIViewController subclass to implement it an load it correctly.
Any suggestion? work around?
I don't like the approach of creating an entirely custom TabBarController from scratch. I like to put a custom view on top of a real TabBar as a subview, and then pass all the button presses to the real TabBarController. This way you don't have to code a window manager yourself.
- (void)tabButtonPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
if (button.tag == HomeButton)
self.tabBarController.selectedIndex = 0;
// etc.
}
This should also be rejection safe.
This has been addressed here: Application frame leaves blank line at the top
But, you could also specify your frame by subtracting 20 from y:
CGRect rect = [[UIScreen mainScreen] applicationFrame];
theView.frame = CGRectMake(rect.origin.x, rect.origin.y - 20, rect.size.width, rect.size.height);
It has been a while since I'm using my custom TabBarViewController with disappearing tabbar and it seems to work properly both on iPad and iPhone.
The main problem that I had was due to an incorrect assignment to the content view frame and probably to a wrong assumption that modalVC were loaded from the current view controller.
First point: the content view should use the bounds of the main view, here is a part of the loadView method of the Root View Controller:
[super loadView];
containerOfControllersView=[[UIView alloc] initWithFrame:self.view.bounds];
Second:before add as a subview a view of a view controller remark to it that its frame should have the same bounds of its new parent view.
theView.frame =self.view.bounds;
Third: modal view controllers should be loaded from the root view controller or the will never have correct size. That's why I've implemented a base abstract class for each view controllers that inherit a protocol that manage the presetation and dismissing of modal viewcontrollers.
Hope this helps someone else.
Andrea
I am presenting a modal view controller. If it matters, it is scrolling up from the bottom. How can I control what portion of the screen it occupies?
EDIT: I have the following in the modal view controller. It's not helping.
- (void)viewDidLoad {
TestResultView *trv = [[TestResultView alloc]initWithTest: [Model m].currentTest];
self.view = trv;
trv.frame = CGRectMake(0, 320, 320, 160);
[trv release];
[super viewDidLoad];
}
You can modify the frame of the view controller, but if you're using UIViewController's -presentModalViewController:animated: method, the view behind will be unloaded once your modal view is finished animating onto the screen (This assumes you're on an iPhone) and you'll see a white screen where your background view should be. iOS assumes that your modal view controller will be a full-screen view controller, and dumps the other view to save memory.
If you really want to show a view over part of the screen, you should instead add the UIView (no UIViewController) to your current UIViewController's view as a subview, and then animate it onscreen yourself. I think something like this would work in your UIViewController class that will present the view:
// Add the view as a subview and position it offscreen just below the current view
UIView *myHalfView = [[UIView alloc] initWithFrame:someAppropriateFrame];
[self.view addSubview:myHalfView];
CGRect offScreenFrame = myHalfView.bounds;
offScreenFrame.origin = CGPointMake(0.0, CGRectGetMaxY(self.view.frame));
// Now animate the view upwards
[UIView beginAnimations:nil context:nil];
// Move the view upwards the height of your sliding view so it's entirely onscreen
myHalfView.center = CGPointMake(myHalfView.center.x, myHalfView.center.y - myHalfView.bounds.size.height);
[UIView commitAnimations];
[myHalfView release];
For bonus points, you could fade the view in by setting
myHalfView.alpha = 0.0;
before the UIView animation block, and setting
myHalfView.alpha = 1.0;
inside the block after animating the center property.
When you're done, you can do something similar but in reverse to slide the view offscreen. You can add an animationDidStop selector to the UIView animation block to be notified when the view has slid off screen so that you can remove it from the view hierarchy.
From an aesthetic point of view, you should also be careful how you do this since having a view slide up is a standard behavior, and if your view looks like a normal view but stops halfway, users may feel (even briefly) that the app has frozen. They'll figure it out, but it will leave a bad feeling about your app if not handled carefully. Mainly, I would avoid using standard full-screen cues like including a UINavigationController at the top of your view to help users understand what's going on. Half-sheets tend to be UIActionSheets on the iPhone, so think in that direction.
That is nice, the above accepted answer explains a nice hack to present subViews which feel like ModalViews, but what if it is an iPad, and i can indeed give it a modalViewController which doesnt cover the entire screen.
In case of iPads, I dont think the underneath view will be unloaded. ( because there are options where we can present the modalView on iPads, which dont cover the entire screen )
ModalViewController in the end is a controller itself, and like any other controller has a root view, whose properties can be editted, if we can get hold of it.
Here is what will give you a custom frame of the ModalView :
MyViewController *viewController = [[MyViewController alloc] init];
viewConroller.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:viewController animated:YES];
//superView of viewController's view is modalViewController's view, which we were after
viewController.view.superview.frame = CGRectMake(x,y,w,h);
//x y w h - can have desired values.
I would add to #dsaw's answer that the superview of the modal view does not seem to rotate its coordinate system in landscape mode. Here is the code that I used in my own app:
MyViewController* modalVC = [[MyViewController alloc] init];
modalVC.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:modalVC animated:NO];
CGRect r = CGRectMake(self.view.bounds.size.width/2 - 236,
self.view.bounds.size.height/2 - 130,
472, 260);
r = [self.view convertRect:r toView:modalVC.view.superview.superview];
modalVC.view.superview.frame = r;
While the superview may not rotate itself with the iPad, it does seem to do the right thing and keep the modal view centered if I rotate the iPad after showing the modal view.
I understand that I can change what interface orientations are supported, but if I want the landscape view to be entirely different, or somewhat different than the portrait view, how do I code for it?
Thank you.
Just load another view controller when the orientation changes. To make things simple, I often use a hidden navigation controller and push and pop the views I want for any particular orientation.
Using a dedicated view controller for each orientation is the easiest approach.
If the only difference is presentation, not controller logic, then you could also try coding a single view controller to swap between two views depending on orientation.
E.g. pseudocode
UIView *landscapeView = ...;
UIView *portraitView = ...;
when orientationChanged
{
if landscape then
[portraitView setHidden:YES];
[landscapeView setHidden:NO];
self.view = landscapeView;
else if portrait then
[landscapeView setHidden:NO];
[portraitView setHidden:YES];
self.view = portraitView;
[self.view setNeedsDisplay];
}
I'm trying to do something that shouldn't be that complicated, but I can't figure it out.
I have a UIViewController displaying a UITableView. I want to present a context menu when the user press on a row. I want this to be a semi-transparent view with labels and buttons.
I could use an AlertView, but I want full control on the format of the labels and buttons and will like to use Interface Builder.
So I created my small view 250x290, set the alpha to .75 and create a view controller with the outlets to handle the different user events.
Now I want to present it.
If I use presentModalViewController two (undesired) things happen
1) the view covers all of the screen (but the status bar).
2) It is semi-transparent, but what I see "behind" it its not the parent view but the applications root view.
Ive tried adding it as a subview, but nothing happens, so Im not doing something right:
RestaurantContextVC* modalViewController = [[[RestaurantContextVC alloc] initWithNibName:#"RestaurantContextView" bundle:nil] autorelease];
[self.view addSubview:modalViewController.view];
Is it possible to do what I want?
Thanks in advance.
Gonso
I'm coding similar thing. My approach include.....
Not using dismissModalViewControllerAnimated and presentModalViewController:animated.
Design a customized full sized view in IB. In its viewDidLoad message body, set the background color to clearColor, so that space on the view not covered by controllers are transparent.
I put a UIImageView under the controllers of the floating view. The UIImageView contains a photoshoped image, which has rounded corners and the background is set to transparent. This image view serves as the container.
I uses CoreAnimation to present/dismiss the floating view in the modal view style: (the FloatingViewController.m)
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor clearColor]];
[UIView beginAnimations:nil context:nil];
[self.view setFrame:CGRectMake(0, 480, 320, 480)];
[UIView setAnimationDuration:0.75f];
[self.view setFrame:CGRectMake(0, 0, 320, 480)];
[UIView commitAnimations];
}
wangii
Thats pretty much the solution I found.
I load the view with loadNibNamed and then just add it on top with addSubView, like this:
//Show a view on top of current view with a wait indicator. This prevents all user interactions.
-(void) showWaitView{
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:#"WaitView" owner:self options:nil];
#ifdef __IPHONE_2_1
waitView = [ nibViews objectAtIndex: 0];
#else
waitView = [ nibViews objectAtIndex: 1];
#endif
CGFloat x = self.view.center.x - (waitView.frame.size.width / 2);
CGFloat y = self.view.center.y - (waitView.frame.size.height / 2);
[waitView setFrame:CGRectMake(x,y,waitView.bounds.size.width,waitView.bounds.size.height)];
[self.view addSubview:waitView];
}
Could you elaborate on points 3 and 4?
What I did to give the view the round rect aspect is put it inside a round rect button.
This code will actually allow you to have a small floating view, but if the view is smaller that its parent, the user could interact with the visible part of the parent.
In the end I create my view with the same size, but kept the code just in case.
Gonso
I would strongly consider using a navigation controller to slide in your subview instead of overlaying it. This is the expected model and any small benefit you may think you'll get by doing it your own way will be greatly offset by the principle of (least) surprise.
If you really really have to do it this way, I believe the trick is to add the first table view as a subview of a transparent "holding" view that the view controller maintains. Then add your new sub view as another subview of that.
Again, if you really want to do this, instead of adding a transparent "holding" view, since this pop-up is essentially modal, I would make it a subview directly of the window.
You might want to put in a transparent black shield behind it to prevent touches on the background and focus input on the popup.
But seriously, consider either popping a controller on the stack or using that alert view. Unless you've hired a $$ designer, it's probably not going to look appropriate on the iPhone.
What I did was create a UIViewController on top of my UINavigation controller in my app delegate and made it a property of a singleton object for convenience:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//--- create root navigation controller
self.window.rootViewController = self.navigationController;
//--- create view controller for popups:
popupViewController = [[BaseViewController alloc] init];
popupViewController.view.backgroundColor = [UIColor clearColor];
popupViewController.view.hidden = true; //for rendering optimisation
[self.window addSubview:popupViewController.view];
[AppState sharedInstance].popupViewController = self.popupViewController;
//--- make all visible:
[self.window makeKeyAndVisible];
return YES;
}
At any point in my app, I can then call e.g.
MyViewController * myVC = [[UIViewController alloc] init];
//... set up viewcontroller and its view...
// add the view of the created view controller to the popup view:
[AppState sharedInstance].popupViewController.view.hidden = false;
[[AppState sharedInstance].popupViewController.view addSubview:myVC.view];
The BaseViewController used on the top just inherits from UIViewController and sets up a full-screen view:
//----- in BaseViewController implementation
- (void)loadView {
//------- create root view:
CGRect frame = [[AppState sharedInstance] getScreenFrame];
rootView = [[VCView alloc] initWithFrame:frame];
rootView.backgroundColor = [UIColor whiteColor];
self.view = rootView;
}