I am trying to set up a UIScrollView so that I can swipe between my 3 view controllers. This is my code in AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.;
UIScrollView *sv = [[UIScrollView alloc] init];
BarsViewController *bvc = [[BarsViewController alloc] init]; // Create BarsViewController
StopwatchViewController *svc = [[StopwatchViewController alloc] init]; // Create StopwatchViewController
TimerViewController *tvc = [[TimerViewController alloc] init]; // Create TimerViewController
[sv addSubview:bvc.view];
[sv addSubview:svc.view];
[sv addSubview:tvc.view];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade]; // Hide status bar
self.window.rootViewController = sv;
[self.window makeKeyAndVisible];
return YES;
}
It gives an error on this line:
self.window.rootViewController = sv;
saying, "Incompatible pointer types assigning to 'UIViewController *' from UIScrollView *'".
However, there is no such thing as a UIScrollViewController, so I don't know what to do.
Basically, I just want the whole screen to be a scroll view which allows me to swipe between my 3 view controllers. How would I go about doing that?
UPD: June, 2015
Swift
The concept remains the same, which is described below in Objective-C section. There is a little change in syntax. To add childviewcontroller use following snippet:
let aViewController = storyboard.instantiateViewControllerWithIdentifier("A") as! AViewController;
addChildViewController(aViewController);
scrollView!.addSubview(aViewController.view)
aViewController.didMoveToParentViewController(self)
Check my Swift Github Sample Code
Objective-C
Create your own custom container view controller (I will call it combinedViewController), which will hold your three controllers in scroll view.
Inherit like you always do UIViewController, then use addChildViewController public API in your new combinedViewController -viewDidLoad: like this:
[self addChildViewController:aViewController];
[self.scrollView addSubview:aViewController.view];
[aViewController didMoveToParentViewController:self];
Here’s what the code does:
It calls the container’s addChildViewController: method to add the child.
It accesses the child’s view property to retrieve the view and adds it to its own view hierarchy. The container sets the child’s size and position before adding the view; containers always choose where the child’s content appears.
It explicitly calls the child’s didMoveToParentViewController: method to signal that the operation is complete.
Do this operation with each of your viewControllers. Afterwards, set your combinedViewController as a rootViewController.
if you need further explanation, feel free to ask.
Reference: Design custom container view controller
Here you are my Objective-C Github sample code
UPD: Thanks #Oliver Atkinson for clarifying that addChildViewController: method also calls the child’s willMoveToParentViewController: method automatically.
Results:
let obj1 = self.storyboard?.instantiateViewControllerWithIdentifier("DocumentsVC") as! DocumentsVC
let obj2 = self.storyboard?.instantiateViewControllerWithIdentifier("AppointmentsVC") as! AppointmentsVC
let obj3 = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardVC") as! DashboardVC
self.containerScrollView.frame = obj2.view.frame
self.containerScrollView.addSubview(obj2.view)
obj2.willMoveToParentViewController(self)
self.addChildViewController(obj2)
self.containerScrollView.addSubview(obj1.view)
obj1.willMoveToParentViewController(self)
self.addChildViewController(obj1)
self.containerScrollView.addSubview(obj3.view)
obj3.willMoveToParentViewController(self)
self.addChildViewController(obj3)
self.containerScrollView.contentSize = CGSizeMake(3*UIScreen.mainScreen().bounds.width, 0)
obj1.view.frame.origin = CGPointMake(0, 0)
obj2.view.frame.origin = CGPointMake(UIScreen.mainScreen().bounds.width, 0)
obj3.view.frame.origin = CGPointMake(2*UIScreen.mainScreen().bounds.width, 0)
Swift 3.0
Based on Sachin answer - bit more generic - just add next element to views array
var views = [ViewController(), ViewController2(), ViewController3(), ViewController4()]
func setupScrollView() {
scrollView.frame = views.first!.view.frame
scrollView.contentSize = CGSize(width: CGFloat(views.count) * width, height: 0)
_ = views.map({ addViewToScrollView($0) })
_ = views.map({ $0.view.frame.origin = CGPoint(x: CGFloat(views.index(of: $0)!) * width, y: 0) })
}
func addViewToScrollView(_ viewController: UIViewController) {
scrollView.addSubview(viewController.view)
views.willMove(toParentViewController: self)
addChildViewController(viewController)
}
Try this git repo, Using this repo you can create a view navigation like Snapchat/Tinder Main Pages.
https://github.com/goktugyil/EZSwipeController
Related
I have requirement like inside the view I need to push some other view controllers. For that I tried to add a one window as subview for that particular view then I have given the desired view controller to root view controller for that window. It is showing correctly but the rootviewcontroller is starting from screen (0, 0) point instead of within the window.
In this picture red color is the view controller and gray one is the view with the origin (0, 100). Below is the code I have tried.
UIWindow *insideWindow = [[UIWindow alloc] initWithFrame: CGRectMake(0, 100, _windowView.frame.size.width, _windowView.frame.size.height)];
insideWindow.backgroundColor = [UIColor yellowColor];
insideWindow.windowLevel = UIWindowLevelNormal;
insideWindow.hidden = NO;
insideWindow.clipsToBounds = YES;
insideWindow.bounds = _windowView.bounds;
_windowView.clipsToBounds = YES;
[_windowView addSubview: insideWindow];
SecondViewController *s = [SecondViewController new];
s.view.bounds = CGRectMake(0, 100, _windowView.frame.size.width, _windowView.frame.size.height);
insideWindow.rootViewController = [SecondViewController new];
[insideWindow makeKeyAndVisible];
It is not a duplicate with how to add multiple UIWindows. Here I can able to add the multiple windows But the problem is inside window the viewcontrollers are not setting properly.
I don't really know what you want to achieve but here we go...
SecondViewController *s = [SecondViewController new];
s.view.bounds = CGRectMake(0, 100, _windowView.frame.size.width, _windowView.frame.size.height);
insideWindow.rootViewController = [SecondViewController new];
You basically create an instance of SecondViewController, then you set the bounds (wrong idea) then you assign as window rootViewController another instance of SecondViewController.
The difference between the frame and bounds of a view: Bounds vs Frame
If you want to go that way, then:
SecondViewController *s = [SecondViewController new];
s.view.frame = CGRectMake(0, 100, _windowView.frame.size.width, _windowView.frame.size.height);
insideWindow.rootViewController = s;
Instead of adding windows to display view controllers why you don't add the "SecondViewController" as child to the controller which has the "gray" view?
SecondViewController *s = [SecondViewController new];
[firstViewController addChildViewController:s];
s.view.frame = grayView.bounds; // or CGRectMake(...)
[grayView addSubview:s.view];
[s didMoveToViewController:firstViewController];
I'm trying to manually (programmatically) lay out views in a UITabBarViewController. I instantiate my UITabBarViewController like this:
MYAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
MYViewController1 *myViewController1 = [[MYViewController1 alloc] init];
myViewController1.title = #"My VC 1";
[tabBarController addChildViewController:myViewController1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = tabBarController;
return YES;
}
MYViewController1.m
- (void)viewDidLoad
{
[super viewDidLoad];
_myView = [[MYView alloc] initWithFrame:self.view.frame];
[self.view addSubview:_myView];
}
MYView.m
- (void)layoutSubviews
{
CGRect descriptionRect, buttonRect;
CGRectDivide(self.frame, &buttonRect, &descriptionRect, 50.f, CGRectMaxYEdge);
_descriptionTextView.frame = descriptionRect;
[self addSubview:_descriptionTextView];
_myButton.frame = buttonRect;
[self addSubview:_myButton];
}
The problem I'm having is that when I get to layoutSubviews, the superview's frame is the full size of the window, so the button is hidden by the tab bar. What am I doing wrong?
Without seeing more code, it appears that you may have some confusion about the use of a UITabBarController.
In the code you have posted above you are initializing a UITabBarController, then a UIViewController, and then you are calling 'addChildViewController:' on the tabBarController. (however, addChildViewController: is an instance method on UIViewController).
Is this in attempt to add the ViewController as a tab of the TabBarController? If so, then try the following code in place of addChildViewController: to see if it gives you the functionality that you are looking for:
tabBarController.viewControllers = #[myViewController1]; // Passing an array of viewControllers will set them as a tabs on the TabBar in the order they are added to the array.
If that doesn't help, then please comment on this answer with more details regarding the desired functionality of your code, and I'll update my answer to assist you as much as I can.
EDIT: Looks like I may have misunderstood your problem. Can you try the following code in play of your subview's initialization:
_myView = [[MYView alloc] initWithFrame:self.view.bounds]; // Try bounds instead of frame.
I am trying to figure out for a while now, how should I use the annotation button to switch
to another mapView. The app I am working on uses MapBox - maps. I checked the exemples
provided by them, but programmatically switching between two maps is there always achieved
through tab bar (which is not the case I want to use).
I am working with storyboards and I understood it quit well, how the segue should be made
in the Interface builder, but I think I am not managing with the programmatically integrated
buttons on map views. I initiated 'id's in both header files and I proclaimed them in the
Identity Inspector as well.
This is the part of the code, where I implement the RMMMapView with the annotation in the
main View Controller - ViewController and it works perfectly:
- (void)viewDidLoad{
[super viewDidLoad];
RMMapBoxSource *onlineSource = [[RMMapBoxSource alloc] initWithMapID:(([[UIScreen mainScreen] scale] > 1.0) ? kRetinaMapID : kNormalMapID)];
_mapView = [[RMMapView alloc] initWithFrame:self.view.bounds andTilesource:onlineSource];
_mapView.tileSource = [[RMMapBoxSource alloc] initWithMapID:(([[UIScreen mainScreen] scale] > 1.0) ? kRetinaMapID : kNormalMapID)];
_mapView.centerCoordinate = CLLocationCoordinate2DMake(0,0);
_mapView.adjustTilesForRetinaDisplay = YES;
_mapView.zoom = 4;
_mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:_mapView];
_mapView.showsUserLocation = YES;
[_mapView setConstraintsSouthWest:[_mapView.tileSource latitudeLongitudeBoundingBox].southWest
northEast:[_mapView.tileSource latitudeLongitudeBoundingBox].northEast];
RMPointAnnotation *annotation = [[RMPointAnnotation alloc] initWithMapView:_mapView
coordinate:_mapView.centerCoordinate andTitle:#"Hello, world!"];
[_mapView addAnnotation:annotation];
}
and this is the part, where I try to call the LowContentMap viewController, from the ViewController - main ViewController:
- (void) prepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender {
if ([segue.identifier isEqualToString:#"Hello, world!"]) {
//LowContentMap *lowContentMap = segue.destinationViewController;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
LowContentMap *lowContentMap = [storyboard instantiateViewControllerWithIdentifier:#"lowContentMap"];
lowContentMap.lcm = _vc;
}}
This is the part of the code, that should be filled in:
- (void)mapView:(RMMapView *)mapView annotationView:(RMPointAnnotation *)annotation calloutAccessoryControlTapped:(UIControl *)control{
[self performSegueWithIdentifier:#"ShowSomeViewController" sender:annotation];
}
It would be really great, if somebody would try to resolve the problem.
I followed the discusion between Noa and Kronos at:
Setting up a detail view controller using a segue
but I still think, the part with the 'id' is something I am doing wrong. Thanks in advance.
1. I think your problem is that you don't know how to display another viewController
You should give the "View Controller Programming Guide" a good read, especially the part "Presenting View Controllers from Other View Controllers"
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
I also recommend to check out some of the awesome WWDC videos that show how to use ViewControllers.
2. How to create and display a viewController
There are many ways to do it. E.g. using the storyboard to create and display the viewController, define a protocol in the child controller, override "performSegueWithIdentifier" to set the delegate and implement the protocol to dismiss the vc.
However, in your case, it seems to make sense to do it all programmatically. Thus, you need to:
a) find the right place to add your action
b) alloc and init your view controller:
MyController *myController = [[MyController alloc] init];
// setup as required, there should be at least a delegate (being able to dismiss the view)
myController.delegate = self;
If you have designed your viewController in a storyboard, you might want to use this alloc/init routine instead:
MyController *myController = [[UIStoryboard storyboardWithName:#"Main.storyboard" bundle:nil] instantiateViewControllerWithIdentifier:#"MyController"];
myController.delegate = self;
c) display your new view controller;
this depends on if you have a navigationController:
[self.navigationController pushViewController:myController animated:YES];
...or if you want to present it modally:
[self presentViewController:myController animated:YES completion:NULL];
d) dismiss when done;
when your other controller is done with whatever it does, it should inform its delegate (implement your own protocol!) that it's done and should be dismissed. The delegate is the original viewController that created (alloc'd/inited) the "myController":
// this method should be defined in a protocol and implemented in the vc that created (and owns) the child view controller
// typically, this is the parent view controller
- (void)done {
[self dismissViewControllerAnimated:YES completion:NULL];
}
if you used a navigationController it's not -dismissViewControllerAnimated:, but
[self.navigationController popViewControllerAnimated:YES];
hope this helps to clarify things
Hello fellow programmers,
First, sorry for the long post. My question is rather simple, but I want to make sure you know what I'm doing and I really don't want to change the basic idea of my approach.
(the following is all done programmatically, no storyboards, no nibs, no navigationcontroller)
I have a RootViewController without an own view. All he does is instantiate other ViewControllers, manage their transitions and push (add) their views into the main window. To position these views correctly, I want to get bounds/frame for one of the RootViewCOntrollers SubControllers. This is how I create the RootViewController from the appDelegate (I started with a empty project)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor grayColor];
self.window.rootViewController = [[UCRootViewController alloc]init];
[self.window makeKeyAndVisible];
return YES;
}
After his initialization, the rootviewController creates a MapViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"RootviewController initialized");
self.mapviewController = [[UCMapviewController alloc] initWithDelegate:self];
self.view = self.mapviewController.view;
[self.mapviewController.view setOpaque:YES];
[self.view bringSubviewToFront:self.mapviewController.view];
[self presentModalViewController:self.mapviewController animated:YES];
self.currentlyActiveController = self.mapviewController;
}
The MapViewController creates a navigationBar and a MKMapView. Right now I set the frames hardcoded, because I'm not able to get the bounds/frame of the window in the viewDidLoad() of the MapViewController When I try to get any infos about bounds/frame, I get 0 returned.
- (void)viewDidLoad
{
NSLog(#"MapviewController initialized");
[super viewDidLoad];
self.isMapViewPushedAside = NO;
// Custom initizialation for navigationBar
[self setupNavigationBar];
// Custom initialization for mapview
[self setUpMapview];
[self trackUserLocation];
// Custom initialization for popupActionsButton
[self setUpPopupButtons];
// Custom tests
[self test];
[self focusLocationOnMap:self.locationManager.location.coordinate];
[self.locationManager stopUpdatingLocation];
}
I've implemented two delegate methods that return frame/bounds (same) for the window. The problem is, I must get those values at the start, not after everything has been initialized. when I call the delegate methods from a button after everything is up, they work as expected.
CGRect frame = [self.mapDelegate frameData];
NSLog(#"width by frame: %f", frame.size.width);
NSLog(#"height by frame: %f", frame.size.height);
CGRect bounds = [self.mapDelegate boundsData];
NSLog(#"width by bounds: %f", bounds.size.width);
NSLog(#"height by bounds: %f", bounds.size.height);
How do I obtain the frame/bounds at the start, that is, before calling my custom "setup" methods..?!
I have a RootViewController without an own view.
You can't have a UIViewController without a view. If you have the app will crash. When you initialize a UIViewController it automatically creates a UIView for you.
- (void)viewDidLoad
{
[super viewDidLoad];
..
self.mapviewController = [[UCMapviewController alloc] initWithDelegate:self];
self.view = self.mapviewController.view;
..
}
From what can I see here, you're actually setting the RootviewController's view to be the map view. This should be done by overriding the -(void)loadView method of your controller and there you need to set the view:
-(void)loadView
{
self.mapviewController = [[UCMapviewController alloc] initWithDelegate:self]; //if you're not using ARC this should be autoreleased;
self.view = self.mapviewController.view;
}
When viewDidLoad method is called there is no geometry set in any of the views of your controller. They are only initialized (implicitly or explicitly by -(void)loadView) and viewDidLoad is called just right after that. Geometry is setup at earliest in viewWillAppear: method and the consecutive viewDidAppear: method, so viewWillAppear: is the earliest point you can have your actual frame/bounds of your views and in viewWillAppear: method you should only execute some lightweight operations (like setting geometry, starting timers, subscribe observers, etc..).
You said you don't want to change your approach, but you need to design according to these rules.
I am working on an iPhone app but found that I require another view / window to get the user to input and save data / information there.
How do I add another view? Do I add it in interface builder and then link it in the main app delegate or will it have its own .h and .m files.
I selected a window view app to start with, do I need to start over with a flip side view app or can this just be added in anyway if I have the correct code there.
manny thanks
Carl
The Window app is perfect for you. In your AppDelegate file, you should have a section like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//instantiate the venue view controller object
YourViewController *yourViewController = [[YourViewController alloc] initWithNibName:#"YourView" bundle:[NSBundle mainBundle]];
// Configure and show the window
[window addSubview:[yourViewController view]];
[window makeKeyAndVisible];
}
This is the part of the code that declares, allocates and adds your custom view to the window. You have a couple choices for how to add the second view. You can either add it in place of this one, or add it after this one using a Navigation Controller. To add the navigation controller, change the above method to look like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//instantiate the venue view controller object
YourViewController *yourViewController = [[YourViewController alloc] initWithNibName:#"YourView" bundle:[NSBundle mainBundle]];
UINavigationController *yourViewControllerWrapper = [[UINavigationController alloc] initWithRootViewController: yourViewController];
// Configure and show the window
[window addSubview:[yourViewControllerWrapper view]];
[window makeKeyAndVisible];
}
There, we create your custom view, then wrap it in a navigation controller. The navigation controller is what gets added to the window. Next the code to switch to the second view would look like this, assuming you switch views on a button press:
-(IBAction)switchViewController{
MySecondViewController *secondViewController = [[MySecondViewController alloc] init];
[self.navigationController pushViewController:secondViewController];
}
Of course, you should replace the line
MySecondViewController *secondViewController = [[MySecondViewController alloc] init];
with the proper way of instantiating your second view controller. This could be from a nib file like above, or programmatically.
As far as creating the view files, you should create a nib in Interface builder for the layout of everything, then create a .h and .m file for the ViewController code itself.
you can also display new frame instead of new view. It is easier sometimes, as you don;t have to pass parameters - you are in one class:
CGRect frame = okresView.frame;
frame.origin.x = frame.size.width;
if ( [okresView superview] == nil )
{
[self.view addSubview:okresView];
}
okresView.frame = frame;
[okresDataTableView reloadData]; // przeładowanie tabeli na subwidoku
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.5];
frame.origin.x = 0;
okresView.frame = frame;
[UIView commitAnimations];
if you want new subview, you can use a few methods - just download few applications from XCode help and check how they do this. Nice example are in 'Elements' and 'UICatalog' application where you have flipped view and other examples.
// Create and push another view controller.
UIViewController *myViewController = [[UIViewController alloc] init];
myViewController.title = #"My First View";
myViewController.view.backgroundColor = [UIColor redColor];
//to push the UIView.
[self.navigationController pushViewController:myViewController animated:YES];