Container/Child UIViewControllers and status bar - ios

I'm trying to get a parent view controller to display a child view controller's view. I've got the following in the parent's viewDidLoad :
-(void)viewDidLoad
{
[super viewDidLoad];
// create child view controller
[self setMenuController:[[MenuViewController alloc] initWithNibName:#"MenuView" bundle:nil]];
[self addChildViewController:[self menuController]];
// move the child view down 30 pt
CGRect frame = [[[self menuController] view] frame];
frame.origin.y = 30;
[[[self menuController] view] setFrame:frame];
[[self menuController] setWantsFullScreenLayout:YES];
[[self view] addSubview:[[self menuController] view]];
[[[self menuController] view] setNeedsLayout];
}
The child view controller's xib is simply a red view with a centered, partially transparent UIImageView (to see overlap).
If I disable the status bar in application:didFinishLaunchingWithOptions:, I get the following:
If I enable the status bar, I get:
With a status bar, the child controller's UIView is automatically adjusted to make room for where the status bar would be (since the image view is centered, that's 10 pts on top and bottom). Since this is a child view, it should always use the full size of the view controller's view regardless of whether the parent will have a status bar. Is there any way to achieve this besides resizing any child views to be 20 pixels bigger, or using a wrapper view in the child view controller?

Try setting frame to menuController's view's bounds instead of its frame. Also, I'm not sure that you want setWantsFullScreen which should cause the view to underlap the status bar.
After Edit:
Try it like this, and see if this works.
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self setMenuController:[[MenuViewController alloc] initWithNibName:#"MenuView" bundle:nil]];
[self addChildViewController:menuController];
[menuController didMoveToParentViewController:self];
menuController.view.frame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y +30, menuController.view.frame.size.width, menuController.view.frame.size.height);
[self.view addSubview:menuController.view];
}

Related

Adding 3 UIViewControllers in Single UIView-programmatically

This is what i am doing
1. I am adding a UIView of width 960 on FirstViewController through xib so that it can accomodate 3 view controller
2. Then I am trying to add 3 viewcontrollers as subview on that view , by programmatically setting the frame
Problem:
While changing the x coordinate to 320 it is not showing the second view..
This is the code :-
Currently i am trying with 2 views
#pragma mark - SetupView View
-(void)setupView
{
//setting up Interval Home Screen on Container View
intervalHome=[[IntervalHomeViewController alloc]initWithNibName:#"IntervalHomeViewController" bundle:nil];
CGRect f=self.containerView.frame;
f.size.width=self.view.frame.size.width;
intervalHome.view.frame=f;
[self.containerView addSubview:intervalHome.view];
[intervalHome.btnAddCustomInterval addTarget:self action:#selector(addCustomIntervalScreen:)forControlEvents:UIControlEventTouchUpInside];
[intervalHome.btnAddInterval addTarget:self action:#selector(addIntervalScreen:)forControlEvents:UIControlEventTouchUpInside];
//setting up AddInterval screen on Container View
addInterval=[[AddIntervalViewController alloc]initWithNibName:#"AddIntervalViewController" bundle:nil];
[self.containerView addSubview:addInterval.view];
CGRect frame=f;
frame.origin.x+=self.view.frame.size.width;
addInterval.view.frame=frame;
addInterval.view.tag=99;
NSLog(#"Frameinterval=%#",self.addInterval.view);
}
#pragma mark - Switch to addInterval view
-(IBAction)addIntervalScreen:(id)sender
{
NSLog(#"Frame=%#",self.containerView);
CGRect frame=self.containerView.frame;
frame.origin.x+=self.view.frame.size.width;
self.containerView.frame = CGRectMake( frame.origin.x, frame.origin.y, frame.size.width,frame.size.height );
NSLog(#"Frame2=%#",self.containerView);
}
you should use UIPageViewController which is made exactly for that scenario :-)

CALayer not changing position in UIScrollview

I have a tableViewController with a dynamic UIView inside it, where it's layer position changes so that it stays underneath the status bar. I did the same to a regular View controller with a UIScrollView.
Where the "status banner" in the tableViewController does what it is suppose to, the status banner in the viewController does not.
The status bar's position is modified in a viewDidScroll method within each view controllers.
Why isn't the banner in the second view controller not moving?
Here is the code, it is the same in both view controllers:
-(void)scrollViewDidScroll: (UIScrollView*)scrollView
{
CALayer *layer = _statusBanner.layer;
float scrollOffset = scrollView.contentOffset.y;
if (scrollOffset > 10)
{
[[self navigationController] setNavigationBarHidden:YES animated:YES ];
[_statusBanner setHidden:NO];
layer.position = CGPointMake(layer.position.x, scrollOffset + 10);
}
else {
[[self navigationController] setNavigationBarHidden:NO animated:YES ];
[_statusBanner setHidden:YES];
}
}
I figured a workaround with this issue. I had decided to leave statusBanner out of the scrollView of the viewController. The banner and the scrollview are both wrapped in the primary UIView of the viewController and the banner is placed just above scrollView.
I was looking too hard at the problem to see the easiest solution.

How to also animate subview in navigation bar?

I have a subview in my navigation bar. I try to add it by this way:
UIView *customView =
[[UIView alloc] initWithFrame:
CGRectMake(0, 0, image.size.width + label.frame.size.width, 44)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[customView addSubview:imageView];
[customView addSubview:label];
[self.navigationController.navigationBar addSubview:customView];
However, when I try to push the navigation bar, the customView stays in the place, not animating following the sliding navigation bar. How can I achieve the animated subview? Is it even possible? Thanks!
you should not add subview in that way
you have tospecify your view location in the left , right or title view
self.navigationItem.titleView = YOURVIEW;
or choose another location left or right items in this way the the title view will added to the current view if you want to remove it just set it to nil in the place you want and reload it subviews again,
self.navigationItem.titleView = nil;
As you are using
[self.navigationController.navigationBar addSubview:customView];
that means the navigation bar you have create is in App delegate and is common for all the viewControllers in your project,that is why once you add a view on to it you see it on every view you have. Remove your sub-view in
-(void)viewWillDisappear:(BOOL)animated{
[Your subview remove fromSuperView];
[super viewWillDisappear:YES];
}
and add that subview in
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController.navigationBar addSubview:Your subview];
[super viewWillAppear:YES];
}
this will add the subview in that particular view only and remove it as soon as that view is popped or pushed.The code given is not correct to the syntax please give a check on that.

How to resize a modalViewController with UIModalPresentationPageSheet

I have a modal view controller that I show with UIModalPresentationPageSheet. The problem is that its default size is too large for my content: so I want to resize its frame to adjust accordingly with my content.
Does anyone know of a way/trick to do this?
Thanks.
Actually you can resize the view controller that gets presented with UIModalPresentationPageSheet. To do it you need to create a custom view controller class and add the following to the class:
<!--The header file-->
#interface MyViewController: ViewController{
//Used to store the bounds of the viewController
CGRect realBounds;
}
<!--In the .m file-->
//viewDidLoad gets called before viewWillAppear, so we make our changes here
-(void)viewDidLoad{
//Here you can modify the new frame as you like. Multiply the
//values or add/subtract to change the size of the viewController.
CGRect newFrame = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height);
[self.view setFrame:newFrame];
//Now the bounds have changed so we save them to be used later on
_realBounds = self.view.bounds;
[super viewDidLoad];
}
//viewWillAppear gets called after viewDidLoad so we use the changes
//implemented above here
-(void)viewWillAppear:(BOOL)animated{
//UIModalpresentationPageSheet is the superview and we change
//its bounds here to match the UIViewController view's bounds.
[super viewWillAppear:animated];
self.view.superview.bounds = realBounds;
}
And then you display this view controller with a UIModalPresentationPageSheet. And that's it. This does work for iOS 5.1.1 and iOS 6 as of the date of this post.
You can't resize a UIModalPresentationPageSheet because its size is not modifable.
This works for resizing a view controller that's presented as UIModalPresentationFormSheet. I would give this a try, I'm not sure if it'll work or not:
navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:navController animated:YES];
//these two lines are if you want to make your view smaller than the standard modal view controller size
navController.view.superview.frame = CGRectMake(0, 0, 200, 200);
navController.view.superview.center = self.view.center;

UITableView: Painting-Behavior of Header- and Footer-View

Why does the header and footer of a tableview always stays on top of the view hierarchy, but not the cells of the table?
Here is what I got:
A table view with custom cells, a footer and a header
A Navigationbar with a menu-Button on the right
When the user taps the menu-button, a semi-transparent menu (UIView) fades in from the top, but not over the navigationbar, only over the view of my tableviewcontroller
When the user tabs a menu-button, the menu slides back to (0,-menuHeight)
But when the menu is over the header, this menu-region is behind my header
I anchored the menu on the view of my tableviewcontroller, beacause I want the navigationbar allways be visible. the solution to anchor it on the navigationbar solves the problem with the header-view, but covers the navigationbar.
Has anyone an idea how to solve this problem? Why are the cells painted correctly?
You should probably not interfere directly with the internal view hierarchy of UITableView. Apple is free to order a table view's subviews as they see fit (and change it in future releases).
Instead, place the table view and your menu view into a common container view and make the latter the main view of your view controller. That way, you can be certain that your menu view will always be above the table view.
I changed my design, but now I do not get touch-events in my table.
But when I add this method - I see, that I'm touching the tableviewcontroller...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"ShieldingViewController received touch");
[self.buttonMenu shieldingViewTouched];
}
Here is what I changed:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewFrame = self.view.frame;
viewFrame.origin.y = 0;
UIView *rootView = [[UIView alloc] initWithFrame:viewFrame];
rootView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Setup the table view
UITableView *newTableView = [[UITableView alloc] initWithFrame:viewFrame style:self.tableView.style];
newTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
newTableView.backgroundColor = [UIColor darkGrayColor];
UIView *menuLayerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
self.tableView = newTableView;
[rootView addSubview:self.tableView];
[rootView addSubview: menuLayerView];
self.view = rootView;
[[CustomNavigationController instance] setCurrentViewForMenu: menuLayerView];
[[CustomNavigationController instance] showMenuInNavigationBarForController:self];
[newTableView release];
[menuLayerView release];
[rootView release];
}

Resources