Reload/Refresh SuperView of the ViewController with transition animation ios4.0+ - ios

I am new to ios development.
Setup
The view hierarchy is as follows:
MyViewController has the following
myView ( UIView)
-> Navigation Bar
-> UiCollectionView
-> UIButton
What is going on ??
RootViewContoller presents myViewController
(things work till this point)
What i want to do after that??
On clicking "UIButton" :- reload the "myView" with a different text in the navigation bar and different content in the "UICollectionView".
Animation requirements : Reload/Refresh of the view should happen with transition effects of the view for eg: flip from left, curl up etc.
->I tried [myView setNeedsDisplay] to see if the view reloads without animation - doesn't work
-(IBAction) onReload {
[self.myView setNeedsDisplay];
}
-> I also tried
[UIView transitionWithView:myView ....
although i don't know what to put in animation block which would trigger the refresh of view.
I would appreciate any help. Thanks

Create a second instance of your view controller, properly setup as far as its title and UICollectionView content are concerned, then when handling your button action, do:
[self.navigationController pushViewController:newViewController animated:NO];
This is the basic way you handle a navigation hierarchy under iOS: you create new controllers and push/pop them to/from the navigation controller.
If you are not using a navigation controller, rather you are trying to implement the same functionality as a navigation controller would handle on your own, I strongly suggest you to use a navigation controller instead... :-)
Hope this helps.

Related

Add common views once in navigation controller

What I'm trying to achieve is to design my "shared" part of UI in interface builder and use child view controllers to show content to the user. This may sound as trying to reinvent UINavigationController, but it is not. In fact, the whole thing is embedded in one.
It looks like this :
Now, what I'm trying to do is change child view controllers of this BaseViewController and indicate this change in the navigation bar, so that all of its functionality remains.
I tried adding such a method :
+ (UIViewController *)baseViewControllerWithChild:(UIViewController *)child {
BaseViewController *base = [BaseViewController new];
[base addChildViewController:child];
child.view.frame = base.childViewControllerContainer.frame;
[base.view addSubview:child.view];
[child didMoveToParentViewController:base];
return base;
}
and then using it like this :
- (void)didTouch:(UIButton *)sender {
[self.parentViewController.navigationController pushViewController:[BaseViewController baseViewControllerWithChild:[DummyViewController new]] animated:YES];
}
(Note : DummyViewController is exactly that - a dummy vc, made just for testing, it only has background color set in viewDidLoad)
This method is a handler of a button in first child view controller. So far so good. Unfortunately, the result is not as expected - the pushed view controller is black. At firs I thought this was because BaseViewController was designed in storyboard and initially set as rootViewController of navigation controller. Moving it to a xib file and setting from code didn't quite work for me, as you cannot add a Container View in a xib.
To summarise, I would like to have a base design governed by BaseViewController class and content would be added as a childViewController of it. Pushing a new view controller would be a result of an action on these childViewController and should update the navigation stack accordingly.
Also, the whole thing needs to work with iOS 7.
Any help as to how to try to achieve this is greatly appreciated!
The issue was casued by base.childViewControllerContainer being nil - this was caused byt he fact that view property of view controllers is loaded lazily. Adding [base view] before accesing base.childViewControllerContainer solved the issue, though I'm not sure if this is the one, only and best way to do this.

How to Push a clear or alpha UIViewCotroller

A button in UIViewController VC1,from this button,we need push(not present) a UIViewController VC2,and VC2's backgroundColor is clearColor or with alpha.
Maybe somepeople will answer me:just setBackgroundColor [UIColor clearColor],if this,the VC2 will be clear or with alpha when it pushing,but after animation complete,it's backgroundColor will be black or white strangely,that's my doubt.I just want VC2 above on VC1,and from VC1 i can see VC1,and just push not present!
You should use the below code :
[viewController2.view setBackgroundColor:[UIColor clearColor]];
Pushing a new view controller will cause the first view controller's view to be removed from display. Setting the background color of the second view controller will not help.
You will likely need to manage the overlay as a subview to the first view controller's view.
"Presenting" a view controller means to display one view controller over another. "Pushing" a view controller means to replace one view controller with another. If you want to keep the other view controller around, you must present the new one, not push it.
If by "push" you mean "slide in from the right," then you really mean you want to present it with a custom transition. This is usually done with a UIPresentationController in iOS 8. In iOS 7, you can do it slightly more by hand using a UIViewControllerTransitioningDelegate to provide the animation you want.
You should take a look at Custom UIViewController Transitions by Ash Furrow for a nice introduction to the iOS 7 way. The iOS 8 is nicely covered (in Swift) by "Custom presentations using UIPresentationController in Swift".
Ok,ultimate solution:
From VC1 push to VC2,VC1 just do this:
step1,
[VC1 addChildViewController:VC2];
[VC1.view addSubview:VC2.view];
[VC1 didMoveToParentViewController:self];
setp2,
VC2 setBackgroundColor clearColor or alpha.

iOS view doesn't display any of it's subviews when added as a subview of NavigationController's view

I have a NavigationController and I want to make a View that animates itself from the top of the screen and sits above navigationBar. I'm initialising viewcontroller from a xib and it works perfectly when I add it as [self.view addSubview:myView]; , but it sits below navigation bar. When I try to add it as [self.navigationController.view addSubview:myView]; it places it on top of navigationBar as intended but view doesn't display any of it's subviews placed in xib. I can't see why this is happening, have been trying to solve this for hours.
UPDATE
My custom view from a xib is paired with custom ViewController class and I added this code in it :
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(#"!!! %i", [self.view.subviews count]);
}
if I add myView to self.view it prints 1 (there's actually 1 subview), but when I add it as self.navigationController.view -viewDidAppear is not even called. But it does appear on the screen with no subviews. Now I'm even more confused.
I think you need use this:
[self.navigationController.navigationBar addSubview:myView];
Why don't you just remove the navigation controller if you don't need it? Another way is to hide the navigation bar so that view occupies complete space and then show the navigation bar again when needed.
I could help more if you provide some code with the views.
Turns out I'm an idiot. It's a really simple scope problem. I was instantiating a local variable of my VC inside the method, and of course it was released at the end. As soon as I assigned my VC to a global variable all started to work like a charm. Easy as that.

What's the proper way to keep UITabBar in all ViewControllers (Don't want to use UITabBarController)

Just to clarify things, I don't want to use UITabBarController. I need to do some custom changes to the UITabBar that can't be done using UITabBarController. (like making it scroll etc')
This is how I've created my UITabBar
From the Interface Builder I've dragged a UITabBar and located it inside a ViewControllers.
Connected the delegate and outlet.
Added UITabbarItem tags and segue identifier.
and used this code:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (item.tag==0) {
[self performSegueWithIdentifier:#"Favorite" sender:nil];
}
else if (item.tag==1)
{
[self performSegueWithIdentifier:#"Item" sender:nil];
}
}
My problem is that when I push a new ViewController the UITabBar disappears.
My question is, what's the right proper way to keep the UITabBar on the pushed ViewController and other ViewControllers ?
I've tried passing it to the next view controller using PrepareForSegue and it works but when I go back to my previous controller I need to reset the UITabBar frame etc'. I guess I can keep it as a global object inside my Singleton and keep adding it to new ViewControllers but that sounds like an over kill
Is there a better way to do it without using a UITabBarController ?
Even if you don't want to use a tab bar controller, you should still follow the same design pattern. Your ScrollableTabBarController should be a container view controller, and when different tab items are selected, it should add the new item as a child view controller. Read the view controller containment documentation for more details.
At the moment it sounds like you're pushing view controllers on top of your container, which suggests that your storyboard is based on a navigation controller. This is the wrong way to do it.
I'm not sure how straightforward it is to do custom container controllers in the storyboard, (I'd do it in code). You may have to make the connections manually rather than via segues.

what's the difference of pushViewController and addSubview

I create a ViewController named "YLJTestViewController" by interface builder ,code is like:
-(IBAction)DoneButtonPressed:(id)sender
{
YLJTestViewController *testViewController = [[YLJTestViewController alloc]initWithNibName:#"YLJTestViewController" bundle:nil];
[self.navigationController pushViewController:testViewController animated:YES];
//[self.view addSubview:testViewController.view];
}
but when I use [self.view addSubview:textViewController.view];it crashed,but use [self.navigationController pushViewController:testViewController animated:YES];it works well,so what's the difference?I thought they are the same...
pushViewController is like adding a piece of paper onto a stack of paper, while addSubView is like gluing a piece of paper onto another paper.
There is no explicit relationships between the previous view and the new view of the view controller which is pushed (like the pieces of paper are still separated in the stack). While the parent view will keep a strong reference to its subviews (like glue).
-addSubview: is a method of UIView. It inserts a view into another view. Like adding a button on a page.
-pushViewController: is a method of UINavigationController. It pushes a view controller onto a navigation stack. Like sliding from a table view to a details view.
In short, -addSubview: composes a view. -pushViewController: is a transition between views.
As sptrakesh states in this Apple Support forum thread:
addSubview is a lower level feature, which you use to add additional
views to your parent/main view. pushViewController replaces the
current main view in your window with the view associated with the new
view controller. You use presentModalViewController when you want to
display a view modally (blocks previous view) on top of your current
view. If you use full screen for your modal view controller, there is
not too much difference between pushViewController and this in terms
how the UI behaves. When you use pushViewController you can "pop" to
any view controller in the array of view controllers that have been
pushed, which is not as easy to do with nested modal views.
In your case the problem is not the use of addSubview: vs. pushViewController:animated:, but simply a typo when you use addSubview:.
[self.view addSubview:textViewController.view]; // misspelled
Should be (replacing x with s)
[self.view addSubview:testViewController.view]; // correct
As for the difference between addSubview: vs. pushViewController:animated:, others have already made good answers. Basically you should use pushViewController:animated: when you replace your entires screen's content, and addSubview: when you add non-full screen UI elements to an existing view.
When we are talking about the view of a UIViewController, pushViewController:animated: should be your preferred method.
I've recently ran into similar problems with addSubview and pushViewController. Everyone here has made great comments, but I would add one things:
Usually addSubview is not used by itself. You usually are using it with presentModalViewController, or in the case of controller containment, addChildViewController.
So in summary:
If you are using navigation controllers, you use pushViewController/popViewController to navigate through your app.
If you are manually switching views, use presentModalViewController.
If you are doing controller containment, use addChildViewController.
If you are using story boards, use Segues.

Resources