UIViewController present another UIViewController - objective-c

I Have a UIViewController 1 on it UIButton (added as subview), after I pressed Button (see pic.1 below) on it adding another UIViewController 2 with animation from the bottom to top after some action:
[[[UIApplication sharedApplication] keyWindow] addSubview:self.view];
And it overlaps UIButton, how can I add this subview that it does not cover UIButton(see pic.2 below)
pic.1:
pic. 2:

You haven't shown the animation code but I assume you add the subview off-screen, then change it's frame (animated) to slide it into view.
Add the new subview using insertSubview:belowSubview: instead, passing your button as the second argument. This way the button will overlap the new view, instead of the other way round. addSubview: always puts the new view on top of any others.
EDIT
From your comments it seems that you're adding the second view controller to the screen using presentModalViewController: so the above method won't work. As far as I know there is no way to keep an element from the original view controller on top of the new view controller's view if you are presenting it this way.
You may have to create a new UIWindow and set it's windowLevel to UIWindowLevelAlert to hold your button. This will keep it on top of any of the views underneath. Add this window as a subview to the main window.

{
SecondViewController *objComing=[[SecondViewController alloc] init];
[self.view addSubview:objComing.view];
objComing.view.backgroundColor=[UIColor blueColor];
objComing.view.frame=CGRectMake(0,420, 320, 0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
objComing.view.frame=CGRectMake(0,0, 320, 420);
[UIView commitAnimations];
}
Put this code in the button action and replace secondViewController with your ViewController.

Related

What is a difference between self.view and self.view .superview of a rootViewController?

I have a view controller which is assigned as a rootViewController like below :
self.viewCntrl = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewCntrl;
And in the same view controller i have a button and i am performing transition animation on button click like this:
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view.superview cache:YES];
but it not works when i write like this:
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
As here self is a ViewController's object, the ViewController which is nothing but assigned to self.window.rootViewController. So why it takes self.view.superview instead of self.view to animate?
Depending on the kind of animation you are trying to execute, possibly the answer is here:
If you want to change the appearance of a view during a transition—for example, flip from one view to another—then use a container view, an instance of UIView, as follows:
Begin an animation block.
Set the transition on the container view.
Remove the subview from the container view.
Add the new subview to the container view.
Commit the animation block.

Change UIViewController self.view frame

I am trying to display a viewController xib view with its view displayed under a fixed header banner (0,0,320,44) which is an imageView added on Application window.
This is because I need it to stick on the screen while I navigate multiple view controllers. Its a requirement of the project I am doing.
So far I have tried:
Resizing the XIB view to the appropriate frame size (0,0,320,416), both with dot notation and setter.
Changing the frame in viewDidLoad with self.view.frame = CGRectMake(0, 44, 320, 416);
but it is not working. Any suggestion?
Set the frame in viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
self.view.frame = CGRectMake(0, 44, 320, 416);
[super viewWillAppear:animated];
}
Just want to add a bit more. The reason for changing view of a UIViewController does not work in viewDidLoad is because of the call [window makeKeyAndVisible], which is usually called after your viewDidLoad. This call will change the frame of the window.rootViewController to equal the window's frame. You can test this by changing the UIViewController's view.frame AFTER [window makeKeyAndVisible], and the changes will stick.
viewWillAppear and viewDidAppear are called within [window makeKeyAndVisible] but after the view's frame got resized.
The caveat is that only the top most controller which is the rootViewController has its view's frame changed automatically. This DOES NOT affect any childViewControllers attached to the rootViewController.

Properly remove view, and add subview

Im trying to add a subview, and then also remove the previous view.
here is what my code looks like:
HowToPlay *LetsPlay = [[HowToPlay alloc] initWithNibName:#"HowToPlay" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:[self view]
cache:YES];
[UIView commitAnimations];
MainViewController *ma = [[MainViewController alloc]init];
[ma.view removeFromSuperview];
[self.view addSubview:LetsPlay.view];
The Mainviewcontroller is the view that its currently on. I want it to dismiss that view, then go ahead and add the new view LetsPlay.
This code runs, and it loads a new view, but when i then load another view from LetsPlay i can see that the mainviewcontroller is still running behind it. I want to permanently dismiss it.
Also im not even sure if im going about this correctly, so if im not could you please give me an example of how to do it correctly.
Thanks :)
You're not going at it the right way: you're creating a new instance of MainViewController (and also of its associated view). You're then attempting to remove this newly created view (call it instance2) from its superview while it hasn't even been added to a view (instance1 has). This is why you're still seeing mainviewcontroller.
Instead, you need to get a hold of the currently running/active MainViewController. I.e. you should be holding on to a reference of that view controller. Then you can call removeFromSuperview on its view.
Hope this helps.

presentModalViewController ontop of subview?

I am trying to recreate the iPhone's tabView, but with my own style, buttons, etc. I didn't want to have to totally redo my app, so I simply added a view to the bottom like this [window addSubview:theToolbar]; theToolbar.frame = CGRectMake(0, 425, 320, 44); in my appDelegate.
However, when trying to do this from a view inside a navigationController theToolbar is over it. Is there anyway to somehow present it to the front?
Here's my code to present the view:
AppSettingsController *appSettings = [[AppSettingsController alloc] initWithNibName:nil bundle:nil];
appSettings.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:appSettings animated:YES];
[appSettings release];
Thanks.
it's impossible show partial views of the viewcontroller. if your want to use the same toolbar, you should retain the toolbar to your appdelegate, and add the toolbar to each viewcontroller when it is in view.
or you should just use uiview's as viewcontrollers

Switching to another UIView, within TabBar

Here is my app setup.
TabBar App with three tab items, tabs 2 and 3 are irrelevant
The TabBarController is located within MainWindow.xib
In interface builder for MainWindow.xib, within the TabBarController
i have the seperate viewControllers for each tab.
Then a view for each view controller underneath ( this can been seen when viewing the object in list view)
The application loads and displays the first tab.
I want to be able to have a button in the view of the first tab that can be clicked that will then animate the following:
the current view fading out
a new view fading in within the SAME tab.
Would the new view require a new viewcontroller or just an additional view under the view controller? or can i create a new view controller and load that with its view?
how would i go about this? just the method is needed.
How would i go about this? Thanks
EDIT
my interface builder list looks like this
Where should i place this new view?
You don't need the additional view controller. Just add additional view in current controller's view and that will be good enough. When user touch up on button make animation that will adjust transparency of views for desired duration. After animation is finished set transparent view as hidden so it won't capture the user actions.
#interface NewsletterViewController {
IBOutlet UIView *view1;
IBOUtlet UIView *view2;
}
In interface builder, add this views to NewsletterViewController->View and connect them with these outlets.
Some code for animation setup:
[UIView beginAnimations: #"Fade" context: nil];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationDelegate: self];
[UIView setAnimationDidStopSelector: #selector(onFadeFinished)];
view1.alpha = 0;
view2.alpha = 1
[UIView commitAnimations];