When presenting a new ViewController using the:
UINavigtionController *nav = [[UINavigationController alloc] initWithRoot(myVC)]
[self presentViewController:nav animated:YES completion:nil];
The UIViewControllerTransitionDelegate is never triggered since I am declaring this in myVC. My question is thus: how do I properly setup a interactive transition using the above initializiation?
EDIT:
I tried to follow the advice given below with no luck yet:
VC1.m :
LTFollowersListViewController *f = [LTFollowersListViewController new];
f.delegate = self;
LTNavigationController *nav = [[LTNavigationController alloc] initWithRootViewController:f];
[self presentViewController:nav animated:YES completion:nil];
VC2.h:
#interface LTFollowersListViewController : UIViewController <UIViewControllerTransitioningDelegate>
#property (nonatomic, strong) id<UIViewControllerTransitioningDelegate> delegate;
VC2.m:
#property (nonatomic, strong) CEPanAnimationController *animationController;
#property (nonatomic, strong) CEHorizontalSwipeInteractionController *interactionController;
in the viewDidLoad: {
self.delegate = self;
self.animationController = [[CEPanAnimationController alloc] init];
self.interactionController = [[CEHorizontalSwipeInteractionController alloc] init];
}
in the bottom of file:
#pragma mark - UIViewControllerTransitionsingDelegate
- (id<UIViewControllerAnimatedTransitioning>)
animationControllerForPresentedController:(UIViewController *)presented
presentingController:(UIViewController *)presenting
sourceController:(UIViewController *)source {
NSLog(#"Reached the animation dismissal");
// allow the interaction controller to wire-up its gesture recognisers
[self.interactionController wireToViewController:self.navigationController forOperation:CEInteractionOperationDismiss];
self.animationController.reverse = NO;
return self.animationController;
}
- (id<UIViewControllerAnimatedTransitioning>)
animationControllerForDismissedController:(UIViewController *)dismissed {
self.animationController.reverse = YES;
return self.animationController;
}
- (id<UIViewControllerInteractiveTransitioning>)
interactionControllerForDismissal:
(id<UIViewControllerAnimatedTransitioning>)animator {
NSLog(#"Reached the animation interaction");
// provide the interaction controller, if an interactive transition is in progress
return self.interactionController.interactionInProgress
? self.interactionController : nil;
}
Any suggestions as to what I am missing here? Thanks!
Did you miss
.h file declare the delegate and protocol ViewController will implement
#interface ViewController : UIViewController<UIViewControllerTransitioningDelegate>
#property (nonatomic, strong) id<UIViewControllerTransitioningDelegate> delegate;
.m file Setting the delegate
self.delegate = self;
and also implement the delegate methods.
Related
I created a NavigationBar and added it to the UIViewController. But after init, the reference turns to nil. I'm new to iOS and OC, I don't know why. Anyone can help? Thank you.
code summary:
#interface ContainerViewController()
#property (nonatomic, retain) UINavigationBar *nav;
#property (nonatomic, retain) UINavigationItem *navItem;
#end
#implementation ContainerViewController
- (instancetype) initWithParams:(NSDictionary *)params {
self = [super init];
if (self) {//...}
return self;
}
- setNavTitle:(NSDictionary *) params {
NSString *title = params[#"title"];
/////////////////////////////////
// here goes wrong
// self.navItem == nil here, why?
/////////////////////////////////
self.navItem.title = title;
}
- (void) viewWillAppear:(Bool)animated {
[super viewWillAppear:NO];
static float navHeight = 64.0;
UIViewController *wvController = [WebView init here];
UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), navHeight)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:title];
nav.items = [NSArray arrayWithObjects: navItem, nil];
///////////////////////////////
// I saved the reference here
//////////////////////////////
[self setNav:nav];
[self setNavItem:navItem];
[self.view addSubview:nav];
[self addChildViewController:wvController];
wvController.view.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - navHeight);
[self.view addSubview:wvController.view];
[wvController didMoveToParentViewController:self];
}
#end
This will be useful for you, kindly check and do
Tutorial point site is very easy to learn some important UI basics if you are working in Objective C
i want to do like on this picture:
i do templateDisplayNode:
#interface TemplateDisplayNode : ASDisplayNode
#property (nonatomic, strong) ASDisplayNode *headerNode;
#property (nonatomic, strong) ASDisplayNode *ContentNode;
- (void)addContentNode:(ASDisplayNode *)contentNode;
#end
#implement TemplateDisplayNode
- (instancetype)init
{
self = [super init];
if (self) {
[self createSubviews];
}
return self;
}
- (void)createSubviews {
[self createHeaderNode];
[self createContentNode];
}
- (void)addContentNode:(ASDisplayNode *)contentNode {
[self.contentNode addSubnode:contentNode];
}
- (void)createHeaderNode {
self.headerNode = [[ASDisplayNode alloc] init];
self.headerNode.backgroundColor = [UIColor blueColor];
[self addSubnode:self.headerNode];
}
- (void)createContentNode {
self.contentNode = [[ASDisplayNode alloc] init];
self.contentNode.style.flexGrow = YES;
[self addSubnode:self.contentNode];
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
CGFloat mainScreenWidth = [UIScreen mainScreen].bounds.size.width;
NSMutableArray *mainStackContent = [[NSMutableArray alloc] init];
if (self.headerNode) {
self.headerNode.style.preferredSize = CGSizeMake(mainScreenWidth, 48);
[mainStackContent addObject:self.headerNode];
}
if (self.contentNode) {
[mainStackContent addObject:self.contentNode];
}
ASStackLayoutSpec *contentSpec =
[ASStackLayoutSpec
stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
spacing:0
justifyContent:ASStackLayoutJustifyContentStart
alignItems:ASStackLayoutAlignItemsStretch
children:mainStackContent];
return contentSpec;
}
#end
and create page of TemplateDisplayNode class:
#interface Page1Node : TemplateDisplayNode
#end
#implement Page1Node
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
[super layoutSpecThatFits:constrainedSize];
}
#end
And use it in ASViewController:
#interface MyViewController : ASViewController
#property (nonatomic, strong) MyCustomASViewControllerWithTableNode *myTableNodeViewController;
#property (nonatomic, strong) Page1Node *page1node;
#end
#implement MyViewController
- (instancetype)init {
self.page1node = [[Page1Node alloc] init];
self = [super initWithNode:self.page1node];
if (self) {
[self createMyTableNodeViewController];
}
return self;
}
- (void)createMyTableNodeViewController {
self.myTableNodeViewController = [[MyCustomASViewControllerWithTableNode alloc] init];
[self.page1node addContentNode:self.myTableNodeViewController.node]; //add TableNode like content on page1node
[self addChildViewController:self.myTableNodeViewController];
[self.myTableNodeViewController didMoveToParentViewController:self];
}
#end
MyCustomASViewControllerWithTableNode:
#interface MyCustomASViewControllerWithTableNode : ASViewController
#property (nonatomic, strong) ASTableNode *tableNode;
#end
...
- (instancetype)init {
_tableNode = [[ASTableNode alloc] initWithStyle:UITableViewStylePlain];
self = [super initWithNode:_tableNode];
if (self) {
_tableNode.dataSource = self;
_tableNode.delegate = self;
}
return self;
}
and when i run it, I see a table in the content field, but I can not scroll the table, because Size frame ContentNode = {0; 0};
How to make layout correctly in order to set the size? If I do the same thing, but without using TemplateDisplayNode, then everything works. I would like to use TemplateDisplayNode to remove a lot of duplicate code for similar pages.
AsyncDisplayKit documentation is not advised to use:
[super layoutSpecThatFits:constrainedSize];
If I remove this line, then I do not go into layoutSpecThatFits in TemplateDisplayNode.
Advise how to be? What solutions can be?
I am learning ios with Stanford CS193P, and have issue in Lecture 8
I created a class named DropItBehavior inheritance from UIDynamicAnimator, but I can not use a UIDynamicAnimator method addChildBehavior,xcode warning like"No visible #interface for "DropItBehavior" declares the selector 'addChildBehavior'"
Here is the .h file
#import <UIKit/UIKit.h>
#interface DropItBehavior : UIDynamicAnimator
- (void)addItem:(id <UIDynamicItem>)item;
- (void)removeItem:(id <UIDynamicItem>)item;
#end
Here is the .m file
#import "DropItBehavior.h"
#interface DropItBehavior()
#property (strong, nonatomic) UIGravityBehavior *gravity;
#property (strong, nonatomic) UICollisionBehavior *collision;
#end
#implementation DropItBehavior
- (instancetype)init
{
self = [super init];
[self addChildBehavior:self.gravity];//Here is the Xcode warning"No visible #interface for "DropItBehavior" declares the selector 'addChildBehavior"
return self;
}
- (instancetype)init
{
self = [super init];
[self addChildBehavior:self.gravity];
return self;
}
- (UIGravityBehavior *)gravity
{
if (!_gravity) {
_gravity = [[UIGravityBehavior alloc] init];
_gravity.magnitude = 0.9;
}
return _gravity;
}
- (UICollisionBehavior *)collision
{
if (!_collision) {
_collision = [[UICollisionBehavior alloc] init];
_collision.translatesReferenceBoundsIntoBoundary = YES;
}
return _collision;
}
- (void)addItem:(id <UIDynamicItem>)item
{
[self.gravity addItem:item];
[self.collision addItem:item];
}
- (void)removeItem:(id <UIDynamicItem>)item
{
[self.gravity removeItem:item];
[self.collision removeItem:item];
}
#end
This is because addChildBehavior is a method of UIDynamicBehavior, not of UIDynamicAnimator. According to CS-193P's lecture notes, your DropIt class should inherit from UIDynamicBehavior instead:
#interface DropItBehavior : UIDynamicBehavior
Looking at the docs, there is no method -[UIDynamicAnimator addChildBehavior:], nor in your subclass.
However, UIDynamicBehavior does.
I would double check you are subclassing the right objects.
The initial ViewController of my app has 2 container views: The first contains a ViewController inside which is a UINavigationController which controls the main content of the app. The second container view of the initial VC is a small strip with UILabels positioned directly below where the the Navigation Bar will be.
The initial VC of the navigation stack of the main content VC has the Navigation Bar hidden. All subsequent VC's will display the Navigation Bar along with the second container view of the initial VC. I would like to animate in the second container VC when the app navigates away from the initial VC of the navigation stack.
My question is, what animation do I need to create to match the animation a Navigation Controller performs when it pushes/pops VC on/off the navigation stack? I'm currently using:
[UIView animateWithDuration:0.27f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.playerCardVC.frame = secondFrame;
} completion:nil];
But it's not matching up exactly when the Navigation Bar animates into view. Any thoughts/ideas would be greatly appreciated.
UPDATE:
I've been searching online for an answer to this issue along with trying to tweak the animation posted in my initial question but cannot seem to perfectly match the navigation bar as it animates into view. Any comments or points in the right direction will be truly appreciated.
It's very tough to understand your view hierarchy and navigation design from the description above, perhaps you can post some screenshots or sketches?
You can override the standard horizontal push/pop animations of a UINavigationController. You do so by defining a custom UINavigationControllerDelegate object and a few other things. See below.
Setup your navController and navControllerDelegate like so:
UINavigationController *navigationController = [[UINavigationController alloc] init];
self.navigationControllerDelegate = [[NavigationControllerDelegate alloc] init];
navigationController.delegate = self.navigationControllerDelegate;
Where the NavigationControllerDelegate class looks like this:
#interface NavigationControllerDelegate : NSObject <UINavigationControllerDelegate>
#end
#import "NavigationControllerDelegate.h"
#import "CrossFadePushAnimator.h"
#import "CrossFadePopAnimator.h"
#interface NavigationControllerDelegate ()
#property (nonatomic, strong) CrossFadePushAnimator* pushAnimator;
#property (nonatomic, strong) CrossFadePopAnimator* popAnimator;
#end
#implementation NavigationControllerDelegate
- (id)init
{
self = [super init];
if (self)
{
self.pushAnimator = [CrossFadePushAnimator new];
self.popAnimator = [CrossFadePopAnimator new];
}
return self;
}
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
if (operation == UINavigationControllerOperationPop)
{
return self.popAnimator;
}
else if (operation == UINavigationControllerOperationPush)
{
return self.pushAnimator;
}
return nil;
}
- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
{
return nil;
}
The pushAnimator looks something like this:
#interface CrossFadePushAnimator ()
#property (nonatomic, strong) id<UIViewControllerContextTransitioning> transitionContext;
#end
#implementation CrossFadePushAnimator
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return AnimationDuration;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
[[transitionContext containerView] addSubview:toViewController.view];
toViewController.view.alpha = 1.0f;
// We are using CAAnimation instead of UIView animation because we need the UIToolbar blur layer to animate
CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:#"opacity"];
fade.fromValue = #0;
fade.toValue = #1;
fade.duration = [self transitionDuration:transitionContext];
fade.removedOnCompletion = YES;
fade.delegate = self;
self.transitionContext = transitionContext;
[toViewController.view.layer addAnimation:fade forKey:AnimationKey];
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
[self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];
self.transitionContext = nil;
}
And the popAnimator looks something like the pushAnimator.
Hope this helps!
It's very tough to understand your view hierarchy and navigation design from the description above, perhaps you can post some screenshots or sketches?
You can override the standard horizontal push/pop animations of a UINavigationController. You do so by defining a custom UINavigationControllerDelegate object and a few other things. See below.
Setup your navController and navControllerDelegate like so:
UINavigationController *navigationController = [[UINavigationController alloc] init];
self.navigationControllerDelegate = [[NavigationControllerDelegate alloc] init];
navigationController.delegate = self.navigationControllerDelegate;
Where the NavigationControllerDelegate class looks like this:
#interface NavigationControllerDelegate : NSObject <UINavigationControllerDelegate>
#end
#import "NavigationControllerDelegate.h"
#import "CrossFadePushAnimator.h"
#import "CrossFadePopAnimator.h"
#interface NavigationControllerDelegate ()
#property (nonatomic, strong) CrossFadePushAnimator* pushAnimator;
#property (nonatomic, strong) CrossFadePopAnimator* popAnimator;
#end
#implementation NavigationControllerDelegate
- (id)init
{
self = [super init];
if (self)
{
self.pushAnimator = [CrossFadePushAnimator new];
self.popAnimator = [CrossFadePopAnimator new];
}
return self;
}
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
if (operation == UINavigationControllerOperationPop)
{
return self.popAnimator;
}
else if (operation == UINavigationControllerOperationPush)
{
return self.pushAnimator;
}
return nil;
}
- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
{
return nil;
}
The pushAnimator looks something like this:
#interface CrossFadePushAnimator ()
#property (nonatomic, strong) id<UIViewControllerContextTransitioning> transitionContext;
#end
#implementation CrossFadePushAnimator
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return AnimationDuration;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
[[transitionContext containerView] addSubview:toViewController.view];
toViewController.view.alpha = 1.0f;
// I'm using CABasicAnimation here for a specific reason, but you could also use the animation method you use above.
CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:#"opacity"];
fade.fromValue = #0;
fade.toValue = #1;
fade.duration = [self transitionDuration:transitionContext];
fade.removedOnCompletion = YES;
fade.delegate = self;
self.transitionContext = transitionContext;
[toViewController.view.layer addAnimation:fade forKey:AnimationKey];
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
[self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];
self.transitionContext = nil;
}
And the popAnimator looks something like the pushAnimator.
Hope this helps!
I'm very new to iOS programming (Coming from Java / C++). I'm trying to set up an app with a TabBarController of which one tab should be a SplitView. I've done my research and I know that UISplitview will not work and everywhere people recommend using the MGSplitViewController. I've looked at the demo but I just can't figure out how to use it without it beeing the app's root view and can't find any sample code that could help
So here is what I do with the classes from the demo in a separate UIViewController class that I afterwards add to the TabBarController: This is my class:
#import <UIKit/UIKit.h>
#import "MGSplitCornersView.h"
#import "RootViewController.h"
#import "DetailViewController.h"
#interface ChannelViewController : UIViewController {
MGSplitViewController *splitViewController;
RootViewController *rootViewController;
DetailViewController *detailViewController;
}
#property (nonatomic, retain) MGSplitViewController *splitViewController;
#property (nonatomic, retain) RootViewController *rootViewController;
#property (nonatomic, retain) DetailViewController *detailViewController;
#end
And this is my desperate try to set it up
- (id)initWithTabBar
{
self = [super init];
//this is the label on the tab button itself
self.title = #"SplitView";
//use whatever image you want and add it to your project
//self.tabBarItem.image = [UIImage imageNamed:#"name_gray.png"];
// set the long name shown in the navigation bar at the top
self.navigationItem.title=#"Nav Title";
self.splitViewController = [[MGSplitViewController alloc] init];
self.rootViewController = [[RootViewController alloc] init];
self.detailViewController = [[DetailViewController alloc] init];
[self.splitViewController setDetailViewController:detailViewController];
[self.splitViewController setMasterViewController:rootViewController];
[self.view addSubview:splitViewController.view];
[self.rootViewController performSelector:#selector(selectFirstRow) withObject:nil afterDelay:0];
[self.detailViewController performSelector:#selector(configureView) withObject:nil afterDelay:0];
if (NO) { // whether to allow dragging the divider to move the split.
splitViewController.splitWidth = 15.0; // make it wide enough to actually drag!
splitViewController.allowsDraggingDivider = YES;
}
return self;
}
I guess I'm doing something wrong with delegates? Or do I have something else mixed up?
Is the demo doing things in the IB that I can't see in the code?
I get the split view but no content and especially no navigation bar with the buttons the demo comes with.
I'd be very thankful for hints or sample code!
Ok manny, here we go. This is my working code for the interface:
#import <UIKit/UIKit.h>
#import "MGSplitViewController.h"
#import "ecbView.h"
#import "ecbCalc.h"
#interface splitMain : MGSplitViewController <UIPopoverControllerDelegate,
MGSplitViewControllerDelegate>
{
IBOutlet UIPopoverController* popoverController;
IBOutlet UINavigationController* naviController;
IBOutlet ecbCalc* viewCalcLeft;
IBOutlet ecbView* euroRatesRight;
UIBarButtonItem* savedButtonItem;
BOOL keepMasterInPortraitMode;
BOOL memoryWasDropped;
BOOL viewLoaded;
}
#property (nonatomic, retain) UIPopoverController* popoverController;
#property (nonatomic, retain) UINavigationController* naviController;
#property (nonatomic, retain) ecbCalc* viewCalcLeft;
#property (nonatomic, retain) ecbView* euroRatesRight;
#property (nonatomic, retain) UIBarButtonItem* savedButtonItem;
#property (nonatomic, readonly) BOOL keepMasterInPortraitMode;
#property (nonatomic, readonly) BOOL memoryWasDropped;
#property (nonatomic, readonly) BOOL viewLoaded;
- (void)dismissPopoverController: (BOOL)animated;
- (void)settingsChanged;
#end
and here excerpts from implementation file:
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
// my initialization...
}
return self;
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
CGRect rectFrame = CGRectMake(0.0, 20.0, 768.0, 1004.0 - 48.0); // being above a tab bar!
viewLoaded = NO;
self.view = [[UIView alloc] initWithFrame:rectFrame];
viewCalcLeft = [[ecbCalc alloc] initWithNibName:#"ecbCalc" bundle:nil];
euroRatesRight = [[ecbView alloc] initWithNibName:#"ecbView-iPad" bundle:nil];
naviController = [[UINavigationController alloc] initWithRootViewController:self.viewCalcLeft];
naviController.navigationBar.barStyle = UIBarStyleBlack;
naviController.title = nil;
viewCalcLeft.title = NSLocalizedString(#"BtnTitleCalc", #"");
viewCalcLeft.view.hidden = NO;
NSUserDefaults* prefs = [NSUserDefaults standardUserDefaults];
if ([prefs objectForKey:#"iPadAlwaysSplitTableView"] != nil)
self.keepMasterInPortraitMode = [prefs boolForKey:#"iPadAlwaysSplitTableView"];
else
self.keepMasterInPortraitMode = YES;
NSArray* theViewControllers = [NSArray arrayWithObjects:self.naviController, self.euroRatesRight, nil];
[self setViewControllers:theViewControllers];
[self setDelegate:self];
[self setShowsMasterInPortrait:keepMasterInPortraitMode];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
// protection because this one is called twice
if (viewLoaded)
return;
[super viewDidLoad];
if (memoryWasDropped)
{
if (!self.keepMasterInPortraitMode && UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
// recreate popover controller
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:self.viewCalcLeft];
}
}
viewLoaded = YES;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
memoryWasDropped = YES;
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[self dismissPopoverController:NO];
self.popoverController = nil;
self.naviController = nil;
self.viewCalcLeft = nil;
self.euroRatesRight = nil;
viewLoaded = NO;
}
My MainWindow.xib has a UITabBarController and the button for splitMain is configured for this class but with an empty xib entry. So creation has to go via loadView. Maybe I could have done the viewDidLoad stuff within loadView ... but so I had to protect viewDidLoad from being called twice. That happens in loadView as soon as the view is instantiated from MGSplitViewController class because the initWithCoder there is calling [self setup]. In that function the frame rect is calculated with self.view.bounds so that viewDidLoad is called again because the view doesn't exist yet. Maybe one could implement a workaround within MGSplitViewController.m but I was too lazy doing that.
To get this working on a tab bar controller please make sure you commit most of the changes that are published on the MGSplitViewController's git page. Good luck.