UIView is shown displaced to the top - cocoa-touch

I have a simple app that has two view controllers. Both of them have a UINavigationBar at the top, as a header. The second UIViewController is displayed as a modal view, when the user clicks on a button on the first one.
When my app first launches, the initial view doesn't completely cover the main UIView and seems "pushed" to the top (see image below).
After I click on the "instructions" button, which displays another view with presentModalViewController:animated:, and dismiss the modal ViewController, everything is displayed correctly.
Anybody knows what I might be doing wrong?
I have nothing in viewWillAppear, and this is my viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
if (!self.model) {
self.model = [[FRRSushiRiceModel alloc] init];
[[self.header.items objectAtIndex:0] setTitle: #"Perfect Sushi Rice: Ingredients"];
}
}
and my application:didFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create and add the main controller (ingredients)
self.ingredientsController = [[FRRIngredientsViewController alloc] init];
[window addSubview:self.ingredientsController.view];
[window makeKeyAndVisible];
return YES;
}
This small project reproduces this behavior:
Test Case

Did you untick the "Wants Full Screen" setting in IB, either for the UINavigationController or UIViewController?

I found the error, guys.
Basically I was trusting the system to correctly set the frame of my views to match the usable portion of the screen. This works when you add it to some controller of controllers (such as UINavigationController), or add it via IB.
If you add your controllers programmatically, you need to set the view's frame explicitly. A good default is:
[[UIScreen mainScreen] applicationFrame]
represents the part of the screen available to applications: the whole screen minus the status bar.

Related

SideMenu black view

I am trying to make a sidebar menu but i have a little problem.
I explain :
I created an UIViewController that i called sideMenuViewController
In my viewController Class (the initial view controller), in the header file, i import my class SideMenuViewController and i wrote :
-(IBAction)openSideMenu:(id)sender;
#property(nonatomic, retain) SideMenuViewController *sideMenu;
The openSideMenu action is associated to the menu button.
I implemented this method like this :
- (IBAction)openSideMenu:(id)sender {
CGRect destination = self.view.frame;
if(destination.origin.x > 0){
destination.origin.x = 0;
}else{
destination.origin.x += SideMenuX;
}
[UIView animateWithDuration:0.4 animations:^{
self.view.frame = destination;
}completion:^(BOOL finished) {
if(finished){
}
}];
}
SideMenuX is a macro : #define SideMenuX 154.4
My viewDidLoad method looks like this :
- (void)viewDidLoad
{
[super viewDidLoad];
_sideMenu = [[SideMenuViewController alloc] init];
[self.view sendSubviewToBack:_sideMenu.view];
// Do any additional setup after loading the view, typically from a nib.
}
The problem is that when i click on the menu button, i get a black screen and not my side menu view.
Thank you in advance !
Two problems:
You are not adding the sideMenu at all. Try adding it to the parent view (self.view.superview), which in your case most likely will be the UIWindow: [self.view.superview insertSubview:_sideMenu.view belowSubview:self.view]; If you are using a navigation controller, use self.navigationController.view instead self.view.
Not sure if you initialized the view with a NIB or the Storyboard (see below if you didn't).
Here is a working example. I created the left view controller inside the storyboard like this:
Throw a View Controller component on the storyboard.
Select the controller on the left column, and go to the Identity Inspector on the right column (alt+cmd+3):
Set the Class to SideMenuViewController
Set the Storyboard ID to SideMenuViewController
Instantiate the controller inside viewDidLoad with
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
self.sideMenu = (SideMenuViewController*)[storyboard instantiateViewControllerWithIdentifier:#"SideMenuViewController"];
then insert it as child of the superview.
(Answering the comment below)
This line is the problem:
[self.view.superview addSubview:_sideMenu.view];
In a NIB based project the superview is UIWindow, but in a Storyboard project, the self.view.superview of a UIViewController is nil. You can solve this, for example, adding a UINavigationViewController. Follow these steps:
Throw in a "Navigation Controller"
Delete the view controller it points to.
Press Ctrl and drag the pointer from the UINavigationController to your view controller, and select "root view controller" on the dialog that appears.
Drag the arrow pointing to your view controller to the UINavigationController (the one that marks the initial view controller, not the one that comes from UINavigationController).
Then change your code to
_sideMenu = [[SideMenuViewController alloc] initWithNibName:#"SideMenuViewController" bundle:nil];
[self.navigationController.view.superview insertSubview:_sideMenu.view belowSubview:self.navigationController.view];
To hide the navigation bar of the UINavigationController, select it in the Storyboard and click Hidden in the Attributes Inspector (alt+cmd+4).
All you're seeing is black because you don't have the side menu view added. Try this:
- (void)viewDidLoad {
[super viewDidLoad];
_sideMenu = [[SideMenuViewController alloc] init];
[self.view addSubview:_sideMenu.view];
[self.view sendSubviewToBack:_sideMenu.view];
}

Draggable/tabbable UIPopOverController?

I don't know if you people are familiar with the Blackboard Learn app for iPad. Well it has PopUp windows to show info.
Well, I would like to implement something similar on a iPad app, and I was wondering if it is possible to doit with UIPopOverControllers...
Basically you can open a view and drag it around and keep it on a border of the screen as a tab to show updates and so.
Is it possible to do something similar with UIPopOverControllers or UIViews (the dragging and the tabbing)?
Cheers from Mexico
UPDATE 1:
Well, I managed to load multiple UIViews, next thing goes to be able to drag 'em around, any suggestions?
UPDATE 2:
I tried to add the UIPanGestureRecognizer in otherView.m (the Pop Up Window), here's how it looks
otherView.h
#interface otherView : UIViewController <UIGestureRecognizerDelegate>{
UIPanGestureRecognizer *drag;
}
#property (nonatomic, retain) UIPanGestureRecognizer *drag;
...
otherView.m
- (void)viewDidLoad {
[super viewDidLoad];
// Drag
drag = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(dragAction)];
[self.view addGestureRecognizer:drag];
[drag release];
}
otherView is displayed in mainWindow directly from the appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch.
// Set the view controller as the window's root view controller and display.
//self.window.rootViewController = self.viewController;
[window addSubview:viewController.view];
[window addSubview:secondView.view];
[self.window makeKeyAndVisible];
return YES;
}
Hope you can help me out
As far as I know there is no mechanism to move a popover around on the screen - they are attached to a bar button or point in a view and that is where they exist until they are dismissed. Popovers also don't allow interaction with views (buttons, text fields etc) in the main view while they are visible.
Sounds like a great user interface concept though, good luck!!

How to open an UIView in fullscreen mode with a tabbar?

this is my problem.
I have a tabBar application.
In first tab, i have a table.
On click on table it shows a modal
view.
To go back from modal view to first
view, i use
[self presentModalViewController:nw animated:YES];
The problem is that if i click on the tabbar while is opened a modal view, it opens second view, but first view's table don't work because the modal view is still opened althoug it appear as closed.
It's a way to open modal view in fullscreen covering the tab bar?
Or also to check if modal view is closed or not from another view?
EDIT:
I tried with all of this code:
nw = [[NewsViewController alloc] initWithNibName:#"NewsViewController" bundle:nil];
nw.modalInPopover = YES;
nw.wantsFullScreenLayout = YES;
nw.hidesBottomBarWhenPushed = YES;
nw.contentSizeForViewInPopover = CGSizeMake(320, 480);
nw.modalPresentationStyle = UIModalPresentationFullScreen;
nw.view.frame = [[UIScreen mainScreen] applicationFrame];
[nw.view setNeedsLayout];
but nothing!!! It wan't go in fullscreen!!
Any idea please?
thanks,
alberto
If the view you're presenting is full screen, this should obscure the tab bar. That said, you might need to re-size the view programmatically so that it's the same size as the UIWindow.
You should be able to do something along the lines of...
[nw setFrame:[[UIScreen mainScreen] applicationFrame]];
[nw setNeedsLayout];
...to achieve this. (Sorry, I'm on a Windows box at the moment, so I can't confirm this. Hopefully someone will provide any tweaks if required.)
You should then dismiss the initial modal view via a delegate method in the originating class. (See the "Dismissing a Modal View Controller" section of Apple's View Controller Programming Guide for iOS.) The originating class would then dispose of the modal view.
Resolved using notification!
When a tab change, i send a notification and close the modal controller.
- (BOOL)tabBarController:(UITabBarController *)tbController shouldSelectViewController:(UIViewController *)viewController {
[[NSNotificationCenter defaultCenter] postNotificationName:#"DataComplete" object:nil];
return YES;
}
In my view classes receive a notification and dismiss the controller!
- (void)downloadDataComplete:(NSNotification *)notif {
NSLog(#"Received Notification");
[self dismissModalViewControllerAnimated:YES];
}
Now it's possibile to reopen a modal view also changing tab!
This is a workaround but works!
alberto.

iPad SplitView Application not taking up entire window

I am working on a split-view based application for the iPad. I created a menu overlay system that has a button that leads into the split-view application. See comment for image
When you click the enter button, however, it results in this:
http://commandoswat.webs.com/Screen%20shot%202010-09-16%20at%201.37.11%20PM.png
Here is my code for the application delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch.
mainMenuController = [[MainMenuController alloc]init];
mainMenuController.sWindow = window;
mainMenuController.sController = splitViewController;
// Add the split view controller's view to the window and display.
[window addSubview:mainMenuController.view];
[window makeKeyAndVisible];
return YES;
}
Here is my code for the MainMenuController Enter Button:
-(IBAction) enterButton{
[self.view removeFromSuperview];
[sWindow addSubview:sController.view];
}
How do I make the split view take up the entire screen?
Any suggestions?
Thanks
Looks to me that your detail view isn't sized properly, and/or doesn't have its autoresizingMask property set appropriately (to have flexible width).

How to switch page on iPhone?

HI, everyone,
I want to ask a question about the iPhone application. I am writing a program. The program will ask the user the enter some information and press enter, after that the program will process and get some information, then the program will display the tab page with 3 tabs.
However, I don't know how to switch the page after the user enter the information, I have write the following code.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview: tabBarController.view]; // add the tab page to the view controller
MyViewController *aViewController = [[MyViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
[self setMyViewController:aViewController];
[aViewController release];
UIView *controllersView = [myViewController view];
[window addSubview:controllersView]; // add the first page to the windows in order to let the user enter info.
[controllersView release];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
If I add the tabBarController first, the controllersView will not appear, if I change the seuqence, it don't know how to make the controllersView disappear to let the tabBarController appears.
And the process is done in the MyViewController.m. Can I
1) remove the controllersView in the MyViewController.m after I complete all the process and
2) display the tabBarController.view ?
Thank you very much.
You need to use a UINavigationController as the root view-controller for your window. You can then push the first UIViewController (MyViewController) on as the top view.
(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
When its time for the tab view to appear push it onto the Navigation Controllers stack. To get back to your top view pop the ViewController off the stack
[self.navigationController popViewControllerAnimated:YES];