Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy Switching of "View Controllers" in Mac Apps (similar to iOS)

I am coming from an iOS background and am new to Mac OSX (coco app) development.

Starting with apple sample code project "Simple Cocoa App" as a base, I want to be able to switch between different "View Controllers" or even just between NSViews in any manner similar to that of iOS apps.

Unfortunately, I could not find a way to accomplish this -- and the internet is rather lacking in resources tied to keywords like "View switching in cocoa apps, switching views in mac apps, mac app dev tutorials"...

Do any of you know a tutorial that, and here's the kicker, actually covers the matter of switching between views? Or perhaps know of any quick way you might be able to explain in your Stack Overflow answer?

Proof Of Concept
A very simple proof-of-concept could be the following. In a simple app, there are two views - Screen 1 (currently displayed) and Screen 2 (hidden off screen):

STEP 1) Start app, Screen 1 appears (contains Label "Screen 1" and Button "Go Next")
STEP 2) Press the Button
STEP 3) Screen 1 slides offscreen as
STEP 4) Screen 2 slides in (contains a single Label "Screen 2")

Thanks.

like image 618
m0rtimer Avatar asked Jan 31 '11 10:01

m0rtimer


People also ask

How do I switch between view controllers?

Right-click the control or object in your current view controller. Drag the cursor to the view controller you want to present. Select the kind of segue you want from the list that Xcode provides.

How many view controllers are possible in iOS app?

There are two types of view controllers: Content view controllers manage a discrete piece of your app's content and are the main type of view controller that you create.

How do I navigate to a view controller while also using SwiftUI?

To achieve this, you would need to create a new struct that conforms to UIViewControllerRepresentable . This acts like a wrapper for UIKit s UIViewController . There is a similar protocol for UIView , UIViewRepresentable . Alternatively, this struct will work if you have your ViewController inside a storyboard.

What is pageview controller in iOS?

The PageViewController is used in many of the iOS applications to let the user navigate between the various pages of the content of the application. The navigation can be controlled programmatically in the application. The PageViewController uses the transition that we specify to animate the change.


2 Answers

Core Animation is not as deeply integrated in OS X as it is on iOS. Therefore you will need to do a lot yourself. What you could do is use the window's contentView as a superview and then do the following to switch to another view ( animated ).

- (void)switchSubViews:(NSView *)newSubview
{
  NSView *mainView = [[self window] contentView];
  // use a for loop if you want it to run on older OS's
  [mainView removeAllSubViews];

  // fade out
  [[mainView animator] setAplhaValue:0.0f];

  // make the sub view the same size as our super view
  [newSubView setFrame:[mainView bounds]];
  // *push* our new sub view
  [mainView addSubView:newSubView];

  // fade in
  [[mainView animator] setAlphaValue:1.0f];
}

This won't give you your desired effect however. If you want it to give it a sorta move effect you have to include QuartzCore.framework and do the following:

- (void)prepareViews
{ // this method will make sure we can animate in the switchSubViewsMethod
  CATransition *transition = [CATransition animation];
  [transition setType:kCATransitionPush];
  [transition setSubtype:kCATransitionFromLeft];
  NSView *mainView = [[self window] contentView];
  [mainView setAnimations:[NSDictionary dictionaryWithObject:transition forKey:@"subviews"]];
  [mainView setWantsLayer:YES];
}

then in the switchSubViews: you only have to use:

[[mainView animator] addSubView:newSubView];
like image 76
Antwan van Houdt Avatar answered Oct 12 '22 05:10

Antwan van Houdt


You may want to check PXNavigationBar by Perspx, it can handle your situation in a much more iOS like.

like image 30
sidyll Avatar answered Oct 12 '22 06:10

sidyll