How to replace UITableView within UIViewController on selection - iphone

I am coding in xCode 4.3
This is my first application.
I have a UIViewController, company logo on top, then search bar and then UITableView in the middle (with product names) and bottom footer image. Now I want that once an item is clicked on UITableView, only TableView is replace with a view showing product details.
Right now I can replace the entire view with following code:
if (!self.prodDetailViewController_)
{
self.prodDetailViewController_ = [[PCS1ProdDetailViewController alloc] initWithNibName:#"PCS1ProdDetailViewController" bundle:[NSBundle mainBundle]];
}
[self presentModalViewController:prodDetailViewController_ animated:YES];
But it just increases my work, because I will have to redo the top bar and bottom bar (which remains same in entire application) in all my views.
Is there a way that I just change the element of my main UIViewController to UITableView.
Thanking you in anticipation.

I'm going to assume you're able to use iOS 5 features here. What you can do is implement a container view controller - much like UINavigationController, but with your own view layout so you can keep logo, search bar, etc. all in place and only transition between views in a part of your view.
I created a new container view controller named ViewController. It has a UIView outlet containerView which is set up in the .xib file, along with a top bar, search bar, and bottom bar (corresponding to the other views you describe in your application). It also has properties tableViewController and detailViewController. In its viewDidLoad implementation, it adds a TableViewController instance as a child view controller. When the table view is tapped, the view controller adds a DetailViewController instance as a child view controller and transitions to it. Tapping a button on the detail view transitions back to the table view, and removes the detail view controller as a child.
Here's my viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableViewController = [[TableViewController alloc] init];
self.tableViewController.delegate = self; // I implement a protocol TableViewControllerDelegate to know when row is tapped
// Fix for origin being 20 by default.
CGRect frame = self.tableViewController.view.frame;
frame.origin.y = 0.0f;
self.tableViewController.view.frame = frame;
[self addChildViewController:self.tableViewController];
[self.containerView addSubview:self.tableViewController.view];
[self.tableViewController didMoveToParentViewController:self];
}
I have a delegate method so that the container knows when a table row is tapped, and does the transition between the table view and the detail view. Its implementation follows:
- (void)tableViewController:(TableViewController *)tvc didSelectIndex:(NSInteger)index
{
self.detailViewController = [[DetailViewController alloc] init];
self.detailViewController.backButtonBlock = [self backButtonBlock]; // This block handles the transiton from detail back to table
CGRect detailStartingFrame = self.detailViewController.view.frame;
detailStartingFrame.origin.x = self.containerView.frame.size.width;
self.detailViewController.view.frame = detailStartingFrame;
[self addChildViewController:self.detailViewController];
[self transitionFromViewController:self.tableViewController
toViewController:self.detailViewController
duration:0.5
options:0
animations:^{
CGRect newTableFrame = self.tableViewController.view.frame;
newTableFrame.origin.x = (-1.0f * newTableFrame.size.width);
self.tableViewController.view.frame = newTableFrame;
[self.containerView addSubview:self.detailViewController.view];
CGRect newDetailFrame = self.detailViewController.view.frame;
newDetailFrame.origin.x = 0.0f;
self.detailViewController.view.frame = newDetailFrame;
} completion:^(BOOL finished) {
[self.detailViewController didMoveToParentViewController:self];
}];
}
As mentioned above, the detail view executes a block when tapping on a back button. I create this block in ViewController here:
- (GoBackButtonBlock)backButtonBlock
{
GoBackButtonBlock block = ^ {
[self.detailViewController willMoveToParentViewController:nil];
[self transitionFromViewController:self.detailViewController toViewController:self.tableViewController duration:0.5 options:0 animations:^{
CGRect newDetailFrame = self.detailViewController.view.frame;
newDetailFrame.origin.x = self.containerView.frame.size.width;
self.detailViewController.view.frame = newDetailFrame;
CGRect newTableFrame = self.tableViewController.view.frame;
newTableFrame.origin.x = 0.0f;
self.tableViewController.view.frame = newTableFrame;
} completion:^(BOOL finished) {
[self.detailViewController removeFromParentViewController];
[self.detailViewController.view removeFromSuperview];
}];
};
return [block copy];
}
That's about all there is to it. Be sure to read the "Implementing a Container View Controller" section of the UIViewController class reference for more details. Hope this helps!

Prepare all of your detail in a View, then add this view to the current screen. That's that.

in your "tableViewDidSelectRowAtIndex" method,On selection of row,just show the particular custom view (which contains details of selected row),and at that same time hide the table view,so u can have the view that u want. And make a back button on that custom view,and on that button action,hide your current view and show the tableview again.

Related

Navigation Controller + segment Controller : Unable to change the view

I have a root view controller which has few buttons. On click of these button, I shown up a tab bar controller with each controller showing the table view controller. I have achieved this. So basically, for each table view controller I have a dedicated class. To go ahead, I replaced the tab bar controller, and added the segmented controller to the navigation title view.
The question is how can I set the view based on the selected index. I am able to set the navigation title to be segmented control but on select I am unable to set the view.
Below is what i have achieved so far.
Note: What matters is a running code, I would do that code optimization later on. I dont want to hidde views. I want to call different view controller class.
RootViewController class (on click of the button, i call the first view controller. So that I can set the segment controller:
-(IBAction) pushSegmentController: (id) sender
{
NSLog(#"My Location Button being clicked/touched") ;
FirstViewController *firstViewController = [[FirstViewController alloc] init] ;
[self.navigationController pushViewController:firstViewController animated:YES];
// Releae the view controllers
[firstViewController release];
}
IN FirstViewController class:
-(void)viewDidLoad {
[super viewDidLoad];
NSArray *viewControllers = [self segmentViewControllers];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Table1", #"Table2"]];
self.navigationItem.titleView = segmentedControl;
[self.segmentedControl addTarget:self
action:#selector(indexDidChangeForSegmentedControl:)
forControlEvents:UIControlEventValueChanged];
self.segmentedControl.selectedSegmentIndex = 0;
}
-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl {
NSUInteger index = aSegmentedControl.selectedSegmentIndex;
if(index ==0) {
UIViewController *table1Controller = [[AustraliaViewController alloc] initWithStyle:UITableViewStylePlain];
**???? HOW SHOULD I SET THE VIEW OVER HERE... AS ITS A PART OF THE NAVIGATION CONTROLLER**
}
else { }
}
Note: I have tried using this option:
[navigationController setViewControllers:theViewControllers animated:NO];
But this option doesnt give me the right result. How should I go ahead with the same as I want to call a view controller class and set its view based on the selected index.
You probably don't want to have one view controller with different views depending on the button index, especially since you already have view controllers for your different screens.
If you want the table view controller to be pushed onto your navigation controller, so it will have a back button that gets you back to FirstViewController, use
-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl {
NSUInteger index = aSegmentedControl.selectedSegmentIndex;
UIViewController *newViewController = nil;
if(index ==0) {
newViewController = [[AustraliaViewController alloc] initWithStyle:UITableViewStylePlain];
} else {
newViewController = [[YourOtherViewController alloc] initWithStyle:UITableViewStylePlain];
}
[self.navigationController pushViewController:newViewController animated:YES];
}
If you'd rather have it slide in from the bottom and you want to handle setting up all necessary user interface (e.g. a dismiss button), replace that last line with
[self presentModalViewController:newViewController animated:YES];
What about?
self.view = table1Controller;
or
[self.view addSubview:table1Controller];
I just saw one more mistake you have done. You are allocating a UIViewController, but initializing it like a tableViewController(with initWithStyle).
If it's a subclass of UITableViewController, alloc it with that, not UIViewController.

Show tab bar after its hidden

Is there any way to show a tab bar after it has been hidden?
Got a tabbar-nav structure. For one of the tabs, I need to hide the tab bar for its 2nd and 3rd level view. But at the same time I will need to show its 1st and 4th view.
The sample code from Elements isn't really applicable here I think.
I've found quite a good pragmatic solution to this problem - make the UITabBarController's view larger than it needs to be, so that the actual UITabBar is clipped by the screen.
Assuming that the tab bar view normally fills its superview, this sort of thing should work:
CGRect frame = self.tabBarController.view.superview.frame;
if (isHidden)
{
CGFloat offset = self.tabBarController.tabBar.frame.size.height;
frame.size.height += offset;
}
self.tabBarController.view.frame = frame;
The tab bar is still showing, but it's off the bottom of the screen, so appears to have been hidden.
It might have performance implications if it causes extra clipping, but so far, it seems to work.
The UIViewControllers that are pushed onto the navigation stack can do the something like the following:
- (void)viewWillAppear:(BOOL)animated {
self.tabBarController.tabBar.hidden = NO; // Or YES as desired.
}
EDIT: Added additional code below to deal with the frame. Don't think I particular recommend this idea since it relies on the internal default view structure of a UITabBarController.
Define the following category on UITabBarController:
#interface UITabBarController (Extras)
- (void)showTabBar:(BOOL)show;
#end
#implementation UITabBarController (Extras)
- (void)showTabBar:(BOOL)show {
UITabBar* tabBar = self.tabBar;
if (show != tabBar.hidden)
return;
// This relies on the fact that the content view is the first subview
// in a UITabBarController's normal view, and so is fragile in the face
// of updates to UIKit.
UIView* subview = [self.view.subviews objectAtIndex:0];
CGRect frame = subview.frame;
if (show) {
frame.size.height -= tabBar.frame.size.height;
} else {
frame.size.height += tabBar.frame.size.height;
}
subview.frame = frame;
tabBar.hidden = !show;
}
#end
Then, instead of using the tabBar.hidden change I originally suggested, do the following:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tabBarController showTabBar:NO];
}
Obviously making sure that the implementation has included the category definition so that 'showTabBar' is known.
You need to implement a delegate method
- (BOOL)tabBarController:(UITabBarController *)tabBarController2 shouldSelectViewController:(UIViewController *)viewController
Inside that you can check which index is selected and show the tab bar
if([[tabBarController.viewControllers objectAtIndex:0] isEqual:viewController])// it is first tab
{
tabBarController.tabBar.hidden = FALSE;
}
I know this is an old post but i think the below code would help to hide the tabbar on the viewcontroller you don't want it on and has the added benefit of automatically readding the tabbar when you come back from that view controller
UIViewController *hideTabbarViewController = [[UIViewController alloc] init];
hideTabbarViewController.hidesBottomBarWhenPushed = YES;
[[self navigationController] hideTabbarViewController animated:YES];

How to remove (pop) a view from OTHER view?

I have 2 views. One is the 'viewheader' and the other is the 'viewContent' (added as subview).
'viewContent' contains a TableView (based on UINavigationController) and the TableView can drill down to a DetailView (UIViewController). (see Image below)
My Question is: How can I remove (pop) the DetailView, with a UIButton FROM the 'headerView'.
The goal is to create a fixed Navigation, and only change (animate) the lower Content.
Thanks
// add "fixed" Header
- (void)loadView {
viewHeader = [[HeaderViewController alloc] initWithNibName:#"HeaderViewController" bundle:nil];
self.view = viewHeader.view;
}
// add content view
- (void)viewDidLoad {
[super viewDidLoad];
viewContent = [[MainContentViewController alloc] initWithNibName:#"MainContentViewController" bundle:nil];
CGRect viewFrame = CGRectMake(0.0f, 164.0f, 320, 316);
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewContent];
[navController setNavigationBarHidden:YES];
[navController.view setFrame:viewFrame];
viewHeader.contentNavView = navController; // <<<< CAN'T POP THE VIEW by passing UINavigationController to "header view"
[self.view addSubview:navController.view];
}
You must somehow send a removeFromSuperview message to the detail view when the button on the header view is tapped (i.e. an IBAction is triggered). You can achieve this by either making the detail view a property of the header view controller to be able to call the removeFromSuperview of the detail view in your header view's IBAction.
Or if you prefer loose coupling (which you should), post a notification from the IBActionin the header view and register for that notification in the detail view with a method that removes the detail view from the header view.
I Found my mistake. The definition of 'contentNavView' inside 'viewheader' was wrong
Changed it from UIViewController to UINavigationController.
#interface HeaderViewController : UIViewController {
UINavigationController *contentNavView;
}
...
Calling
[contentNavView pushViewController:aViewController animated:NO];
works.

Transparent Background with a Modal UIViewController

I have a dilema, I want to present to the user a semi-transparent view.
I found out by experimenting that if I simply pushed the transparent view to the top of my NavigationController's stack, that it would not render the transparency level I wanted. So I decided to simply add the view as a subview of the current view at the top of the stack.
This solution works, the view below is still visible, and the View is 'semi-modal'. The problem is, if the parent view inherits from UITableViewController (as mine does), then the view I 'push' onto it, does not cover the navigation bar at the top.
I really don't want to get into a situation where I am forced to enable / disable controls on the navigation bar every time I push this view, so I was wondering, if anyone knew of any solutions that I could use so that the view I push onto the UITableViewController will actually 'push over' the navigation bar?
Funny, I was just doing the same thing yesterday. Unfortunately it seems to be impossible. Once the modal view controller is in place, the previous view becomes hidden.
See this previous question on the topic.
You can still use the view controller and NIB files you have set up - here's my sample code
- (void)showUpgrade {
[self.upgradeVC viewWillAppear:NO];
[self.view addSubview:self.upgradeVC.view];
[self.upgradeVC viewDidAppear:NO];
}
- (void)hideUpgrade {
[self.upgradeVC viewWillDisappear:NO];
[self.upgradeVC.view removeFromSuperview];
[self.upgradeVC viewDidDisappear:NO];
}
- (UpgradeViewController *)upgradeVC {
if (_upgradeVC == nil) {
_upgradeVC = [[UpgradeViewController alloc] initWithNibName:[NSString stringWithFormat:#"UpgradeView_%#", self.deviceType] bundle:nil];
_upgradeVC.delegate = self;
}
return _upgradeVC;
}
You will need to store a reference to the parent view controller in the modal view controller so that you can access the -hide method. I did this through a delegate.
It would also be easy to add some animation to -show and -hide if you want it to animate up from the bottom of the screen - I was just too lazy to do this.
iOS 8 added the UIModalPresentationOverFullScreen presentation style. Set this as the presented view controller’s modalPresentationStyle. For more advanced needs, look into creating a custom presentation controller.
There is now a way to achieve this using iOS7 custom transitions :
MyController * controller = [MyController new];
[controller setTransitioningDelegate:self.transitionController];
controller.modalPresentationStyle = UIModalPresentationCustom;
[self controller animated:YES completion:nil];
To create your custom transition, you need 2 things :
A TransitionDelegate object (implementing
<UIViewControllerTransitionDelegate>)
An "AnimatedTransitioning" object
(implementing <UIViewControllerAnimatedTransitioning>)
You can find more informations on custom transitions in this tutorial : http://www.doubleencore.com/2013/09/ios-7-custom-transitions/
Try this:
ViewController *vc = [[ViewController alloc] init];
[vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self presentViewController:vc animated:YES completion:nil];
Have you tried looping over the Modal View Controller's subviews and setting the background color to clear for every view? This is a DFS recursive function.
- (void)setBackgroundToClearForView:(UIView *)view {
if ([view subviews]) {
for (UIView *subView in [view subviews]) {
[self setBackgroundToClearForView:subView];
}
}
if ([view respondsToSelector:#selector(setBackgroundColor:)]) {
[view performSelector:#selector(setBackgroundColor:)
withObject:[UIColor clearColor]];
}
}
To use it call:
[self setBackgroundToClearForView:self.view];
in viewDidLoad.
This will do the trick.. Try this one.
// for clear color or you can easily adjust the alpha here
YourVC *vc=[[YourVC alloc]initWithNibName:#"YourVC" bundle:nil] ;
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
So that the view will be full screen unlike UIModalPresentationFormSheet..

Transparent Modal View on Navigation Controller

I'm trying to create a transparent modal View on top of my navigation controller. Does anyone know if this is possible?
A modal view will cover the view it is pushed on top of as well as the navigation bar for your navigation controller. However, if you use the -presentModalViewController:animated: approach, then once the animation finishes the view just covered will actually disappear, which makes any transparency of your modal view pointless. (You can verify this by implementing the -viewWillDisappear: and -viewDidDisappear: methods in your root view controller).
You can add the modal view directly to the view hierarchy like so:
UIView *modalView =
[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
modalView.opaque = NO;
modalView.backgroundColor =
[[UIColor blackColor] colorWithAlphaComponent:0.5f];
UILabel *label = [[[UILabel alloc] init] autorelease];
label.text = #"Modal View";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
[label sizeToFit];
[label setCenter:CGPointMake(modalView.frame.size.width / 2,
modalView.frame.size.height / 2)];
[modalView addSubview:label];
[self.view addSubview:modalView];
Adding the modalView as a subview to the root view like this will not actually cover the navigation bar, but it will cover the entire view below it. I tried playing around with the origin of the frame used to init the modalView, but negative values cause it to not display. The best method that I found to cover the entire screen besides the status bar is to add the modalView as a subview of the window itself:
TransparentModalViewAppDelegate *delegate = (TransparentModalViewAppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.window addSubview:modalView];
The easiest way is to use modalPresentationStyle property of navigationController (but you'll have to make animation by yourself):
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:modalViewController animated:NO];
modalViewController.view.alpha = 0;
[UIView animateWithDuration:0.5 animations:^{
modalViewController.view.alpha = 1;
}];
I accomplish this most easily by setting up an "OverlayViewController" that sits above all other subviews of my window or root view. Set this up in your app delegate or root view controller, and make OverlayViewController a singleton so that it can be accessed from anywhere in your code or view controller hierarchy. You can then call methods to show modal views, show activity indicators, etc, whenever you need to, and they can potentially cover any tab bars or navigation controllers.
Sample code for root view controller:
- (void)viewDidLoad {
OverlayViewController *o = [OverlayViewController sharedOverlayViewController];
[self.view addSubview:o.view];
}
Sample code you might use to display your modal view:
[[OverlayViewController sharedOverlayViewController] presentModalViewController:myModalViewController animated:YES];
I haven't actually used -presentModalViewController:animated: with my OverlayViewController but I expect this would work just fine.
See also: What does your Objective-C singleton look like?
I had this same problem and in order to The solution is to add the modal view with addSubview: and animate the change in the view hierarchy with UIView’s animateWithDuration:delay:options:animations:completion:
I added a property and 2 methods to a subclass of UIViewController (FRRViewController) that includes other functionalities. I will be publishing the whole stuff on gitHub soon, but until then you can see the relevant code below. For more info, you can check my blog: How to display a transparent modal view controller.
#pragma mark - Transparent Modal View
-(void) presentTransparentModalViewController: (UIViewController *) aViewController
animated: (BOOL) isAnimated
withAlpha: (CGFloat) anAlpha{
self.transparentModalViewController = aViewController;
UIView *view = aViewController.view;
view.opaque = NO;
view.alpha = anAlpha;
[view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIView *each = obj;
each.opaque = NO;
each.alpha = anAlpha;
}];
if (isAnimated) {
//Animated
CGRect mainrect = [[UIScreen mainScreen] bounds];
CGRect newRect = CGRectMake(0, mainrect.size.height, mainrect.size.width, mainrect.size.height);
[self.view addSubview:view];
view.frame = newRect;
[UIView animateWithDuration:0.8
animations:^{
view.frame = mainrect;
} completion:^(BOOL finished) {
//nop
}];
}else{
view.frame = [[UIScreen mainScreen] bounds];
[self.view addSubview:view];
}
}
-(void) dismissTransparentModalViewControllerAnimated:(BOOL) animated{
if (animated) {
CGRect mainrect = [[UIScreen mainScreen] bounds];
CGRect newRect = CGRectMake(0, mainrect.size.height, mainrect.size.width, mainrect.size.height);
[UIView animateWithDuration:0.8
animations:^{
self.transparentModalViewController.view.frame = newRect;
} completion:^(BOOL finished) {
[self.transparentModalViewController.view removeFromSuperview];
self.transparentModalViewController = nil;
}];
}
}
Here's what I did to solve the problem - Google the details but this approach worked very well for me:
Take a screenshot of the underlying view. https://devforums.apple.com/message/266836 - this leads to a ready-made method that returns a UIView for the current screen.
Hand the screenshot to the modal view (I used a property)
Present the modal view
In the modal view controller's viewDidAppear, set the image as UIImageView at index 0. Adjust the vertical position of the image by the height of the status bar.
In the modal view controller's viewWillDisappear, remove the image again
The effect is:
The view animates in as any modal view does - the semi transparent parts of the modal view glide over the existing view
As soon as the animation stops, the background is set to the screenshot - this makes it appear as if the old view is still underneath even though it isn't.
As soon as the modal view's disappear animation starts, the image is removed. The OS meanwhile shows the old navigation view so the modal view transparently glides away and out of sight as you'd expect.
I tried animating in my own overlay view but it didn't work very well. I got a crash with no indication as to what has crashed. Rather than chase this down I did the bg view & Works really well.
Code in the modal view - I think you can figure out the rest, namely setting the property modalView.bgImage...
- (void)viewDidAppear:(BOOL)animated {
// background
// Get status bar frame dimensions
CGRect statusBarRect = [[UIApplication sharedApplication] statusBarFrame];
UIImageView *imageView = [[UIImageView alloc] initWithImage:self.bgImage];
imageView.tag = 5;
imageView.center = CGPointMake(imageView.center.x, imageView.center.y - statusBarRect.size.height);
[self.view insertSubview:imageView atIndex:0];
}
- (void)viewWillDisappear:(BOOL)animated {
[[self.view viewWithTag:5] removeFromSuperview];
}
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:newview animated:YES];
and make sure you setup the modal view background to be transparent,
self.view.background = .... alpha:0.x;
if you set modalPresentationStyle for the modal view controller to:
viewController.modalPresentationStyle = 17;
The view in the background is not removed. (TWTweetComposeViewController use it).
I did not try to pass App Store review with this code though
This post about displaying a semi-transparent "Loading..." view might give a few pointers on how to proceed.
Yeah, you have to add the view manually, and if you want to slide in from the bottom or whatever you have to do the animation yourself too.
I wrote a class to do this, and a semi-modal datepicker using that class as an example.
You can find documentation in this blog post, the code is on github
I've been researching this same issue for the past week. I tried all the various answers and examples found in Google and here on StackOverflow. None of them worked that well.
Being new to iOS programming, I wasn't aware of something called UIActionSheet. So if you're trying to accomplish this in order to show a modal overlay of buttons (such as a modal asking someone how they want to share something), just use UIActionSheet.
Here is a webpage that shows an example of how to do this.
I got this idea from https://gist.github.com/1279713
Prepare:
In the modal view xib (or scene using storyboard), I setup the full-screen background UIImageView (hook it with the .h file and give it a property "backgroundImageView") with 0.3 alpha. And I set the view (UIView) background color as plain black.
Idea:
Then in "viewDidLoad" of the modal view controller I capture the screenshot from the original status and set that image to the background UIImageView. Set the initial Y point to -480 and let it slide to Y point 0 using 0.4-second duration with EaseInOut animation option. When we dismiss the view controller, just do the reverse thing.
Code for the Modal View Controller Class
.h file:
#property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;
- (void) backgroundInitialize;
- (void) backgroundAnimateIn;
- (void) backgroundAnimateOut;
.m file:
- (void) backgroundInitialize{
UIGraphicsBeginImageContextWithOptions(((UIViewController *)delegate).view.window.frame.size, YES, 0.0);
[((UIViewController *)delegate).view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
backgroundImageView.image=screenshot;
}
- (void) backgroundAnimateIn{
CGRect backgroundImageViewRect = backgroundImageView.frame;
CGRect backgroundImageViewRectTemp = backgroundImageViewRect;
backgroundImageViewRectTemp.origin.y=-480;
backgroundImageView.frame=backgroundImageViewRectTemp;
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^{
backgroundImageView.frame=backgroundImageViewRect;
} completion:^(BOOL finished) {
}];
}
- (void) backgroundAnimateOut{
CGRect backgroundImageViewRect = backgroundImageView.frame;
backgroundImageViewRect.origin.y-=480;
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^{
backgroundImageView.frame=backgroundImageViewRect;
} completion:^(BOOL finished) {
}];
}
In viewDidLoad, simply call:
[self backgroundInitialize];
[self backgroundAnimateIn];
In anywhere we dismiss the modal view controller, we call:
[self backgroundAnimateOut];
Please note that this will ALWAYS animate the background image. So if this modal view controller transition style (or the segue transition style) is not set to "Cover Vertical", you may not need to call the animation methods.
I finally accomplished this, for a navigation or tab bar interface, by combining an overlay view controller (see: pix0r's answer) that's hidden / un-hidden before hiding or showing a view controller based on this very good blog post.
Concerning the view controller, the tip is to make its background view the clearColor, then the semi-transparent overlay view is visible and whatever views are added as subviews in the view controller are in front and most importantly opaque.
I've created open soruce library MZFormSheetController to present modal form sheet on additional UIWindow. You can use it to present transparency modal view controller, even adjust the size of the presented view controller.
For iOS 8+ you can use UIModalPresentationOverCurrentContext presentation style for presented view controller to easy achieve desired behavior.
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.9f];
viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:viewController animated:YES completion:nil];
If you also need to support iOS 7 - check this thread.
You can achieve transparent/semi-transparent modal view effect by overlaying a transparent/semi-transparent button on both the view and the navigation bar.
You can access the navigation bar through the navigationBar property of the UINavigationController.
I found that UIButton unlike UILabel will trap mouse events - hence giving the correct modal behavior.
I just found a workaround for that. Just create a 1X1 of UIViewController and add it to your parent view controller. And show the transparent modal view controller in that UIViewController.
on viewDidLoad;
self.dummyViewController = [[UIViewController alloc] init];
[self.dummyViewController.view setFrame:CGRectMake(0, 0, 1, 1)];
[self.view addSubView:self.dummyViewController.view];
when you need to open a transparentViewController;
[self.dummyViewController presentModalViewController:yourTransparentModalViewController animated:true];
If you need a screen like the attached one, the below code may help you.
The code:
MyViewController * myViewController = [[MyViewController alloc] initWithNibName:nibName bundle:nil];
UINavigationController * myNavigationController = [[UINavigationController alloc] initWithRootViewController: myViewController];
myNavigationController.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentModalViewController: myNavigationController animated:YES];
If say you want a screen overlay, use the parentViewController.view, it will place above navigation bar ++
MyCustomViewController* myOverlayView = [[MyCustomViewController alloc] init];
[self.parentViewController.view addSubview:myOverlayView];
This worked for me:
UIViewController *modalViewController = [[UIViewController alloc] init];
modalViewController.view.backgroundColor = [UIColor whiteColor] colorWithAlpha:0.5];
[self showDetailViewController:modalViewController sender:nil];