Multiple UIView in one UIViewController - ios

I am developing a iPad App.
There is one UIViewController which should show all relevant information.
Inside this VC there are 2 UIViews: The first UIView on the first half of the Screen shows a Image and some text. The second View on the bottom half of the screen should display some stuff like a map or a image gallery depending on the button that was pressed in the center of the screen.
My first try was to load for example a GalleyViewController and then get his View and add this as a Subview to my BottonView like this:
-(void)galleryButtonPressedInCenter{
GalleryViewController * gal = [[GalleryViewController alloc] initWithNibName:#"GalleryViewController" bundle:nil];
[gal.view setFrame:CGRectMake(0,0,620,300)];
[self.bottomView addSubview: gal.view];
But this will crash my app if I touch the View with my finger.
To show what I want to achieve:
On the last Screenshot (http://itunes.apple.com/de/app/ebookers-hotels-furs-ipad/id465464927?mt=8) you can see that in this App there is a ImageView and some other Stuff in the upper part and in the lower part there are multiple Views that are switched depending on the button that was pressed.

Make an empty fullscreen view.
then add your subviews to that view.
ViewController
-UIView (your new umbrella view)
-UIView (subView 1)
-UIView (subview 2)
I don't think I've seen it documented in the helpfiles, but I'm finding that each "ViewController" can only have one UIView directly attached. I'm kinda new at this though, try it anyway :)

Related

When to use UIViewController vs UIView when animating?

When should a UIView be placed over the current views (in the same controller) vs adding a whole new UIViewController?
I know that the UIViewController manages UIViews, but imagine that you wanted something to popup when a user selected something. What decides whether a UIView be animated and added to the current controller (using animateWithDuration), or a new UIViewController with a custom transition?
Twitter example:
http://i.giphy.com/3o7TKKcmcIz76ONXFe.gif - Not sure if this is a UIView added to the existing controller, or if it's a new controller. I just tap the image, and it pops up. I then slide down and slowly move the image off the screen (interactively).
Spotify examples:
http://i.giphy.com/3oz8xRSkmxbcVqPZf2.gif - Probably a UIViewController. I'm grabbing the bottom bar and sliding it up. I then slide down on the album cover.
http://i.giphy.com/3oriO7SjzB5GfThx60.gif - Possibly a UIView? Happens when I press and hold a album cover.
What determines when one should be used over the other? It would probably be helpful if you explained the examples. Thanks.

Endless Controllers Cloning

I am newbie in iOS programming. I've been looking for this information couple days and I still don't have the best answer, so I ask you for help. Please don't comment it like "Search on google.." or so. I have already did this research.
I have an app with 2 Controllers on start. First is a ViewController and it's a simple TableView. Second one is a controller with navigation bar with two buttons on it - burger on the left and plus on the right. When burger is tapped, first ViewController (with TableView) is opened. This is all very simple, but there's a problem with that plus button - it should add a new controller, which is same as the second controller (same buttons, images, etc.) except 2 labels, that must be different. I don't know if the controller has to be ViewController or NavigationController because I want to have 2 custom buttons on navigation bar (burger, plus). I found something about ContainerView, but I'm not sure, if it's the right way.. I want to switch between controllers using slide gesture.
Problems:
every next controller has a "plus" button => it can make a new controller.
How to make all the controllers looks like the same.
I'm adding an image for better understanding: IMAGE
Looking at your image, a simple solution I will use is using UIScrollView, make paging enabled.
Put your tableView and first view controller at the first two pages, and make sure view controllers are all added to the main view controller's childViewController.
When plus button tapped, you expand your the scrollView, make the content size bigger, and add a new controller's view to the scrollView, and add the viewController to the mainViewController's childViewController, keep doing that when new view controller needs to be added, and remember to call 'setContentOffset' to scroll to the new page.
Because paging is enabled, you can swipe between pages with smooth animation that will stop at each page.
As for your problems, to make the same view controller and add new view controller when tap add button, that are the second step.
1. Create new view controller:
If you are using storyboard, you need to give the view controller an identifier, and initialize the view controller with:
UIViewControlller *viewController = [[UIStoryboard storyboardWithName:#"your_story_board_name"
bundle:nil] instantiateViewControllerWithIdentifier:#"you_vc_id"];
If you are using xib:
UIViewController *viewController = [[UIViewController alloc] initWithNibName:#"your_xib_file_name" bundle:nil]
If you are using code, just use the
[UIViewController new]
to create the viewController, they are created with the same source, they surely look the same.
2. To add the view controller the the next page:
Let's assume your are currently at the second page, that's the init state of your app, first page is the tableView.
If the bounds of your scrollView has size of screen size: size1, and every page has the same view size: size1, and when there are two pages, the content size is size2, we know
size2.width == size1.width * pages
at this point, the
size2.width == size1.width * 2
So, when a new page need to added, you need to increase the width of content size by size1.width, and set the new viewController.view's frame to
(CGRect){(CGPoint){size1.width * page, 0},size1}.
Make the add new page a common function, call it when the add button taps.
Hope it helps

iOS Story: Not Enough Room for UIViews In The Sam View Controller

In my app I have a shopping cart, when the user clicks the quantity button, I essentially want the picker (and an accompanying black background), to take up the entire screen space. How do I design this in storyboard? How do I place two full screen UIViews in one ViewController? There isn't enough space for my tableview and picker view to fit in the same view controller. And it doesn't seem like I can drag a UIView directly in storyboard.
Its possible to add views inside your main view of the view controller in storyboard.
Did you try to drag and drop it inside the view?
You can also create a view entirely in code as follows [i like do to it in storyboard as that makes adding constraints a piece of cake]:
Subclass UIView to customize your view ... say in class MyView
Then from your view controller's ViewDidLoad method you can create and add the view as such
self.myView = [[MyView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
[self.view addSubview:self.myView];
// add constraints as needed]
If you are adding multiple views you will have to be careful about the ordering of the views especially if they are full page. A full page view will hide all other views under it.
The recommended way is that if you want the user action to show a full page view, you should probably create a New ViewController instead of a view.
Hth

Generic way to detect a touch outside UIView frame

I have UIViewController with a custom UIView inside (lets call them VC and button). When button is touched, it's bounds and center changes with animation (it becomes bigger and presents a few options to choose from, after choice it resizes back). I would like to know how to "detect" (and dismiss the default action of that touch) a touch outside of the button (just to "hide" the button, in particular to make button resize to default smaller size).
Is there any generic way I could do that? VC has a lot of objects inside its view (table views, buttons, text fields, custom charts made with quartzcore, etc), do i need to block "interactivity" on all of those elements during the "big mode" of button?
You could subclass what ever view you are using and expand the coordinates that will correspond to a touch in the view by implementing
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
Read more in the docs
http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/hitTest:withEvent:
This way of implementing it is suggested in some WWDC videos from last year
Add a UIButton having the frame CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height) as the first subview to your mainview (self.view) , add a selector to this button (#selector(backgroundTap)) , now add all other subviews like UITableView , UIButton , etc (as per your requirment) . Now whenever you will click on to the blank space where no Subview is present the backgroundTap selector will be called.

Create a swipe feature within an UIImageView

In my current test app I have a UITabBarController, linked to a UINavigationController that leads to a UIViewController with 6 buttons in it and a second UIViewController with a UIImageView. I've created a segue for each button, wich works.
I want to be able to create a swipe pictures feature inside that UIImageView. The source pictures are inside the app's folder.
I'm not sure about your question.
If you need to create a sort of slider of images, you could create a UIViewController with a UIScrollViewas the main view (or as a subview of UIViewController's view).
Once set up, you could create a for-loop where you create UIImageView elements to wrap the image you are loading from your app folder and add each UIImageView to the scroll view.
To make possible to snap photos within the UIScrollView set its property pagingEnabled to YES.
For further info you could take a look at Scrolling sample provided by Apple.
Hope this helps.

Resources