How do you add a UICollectionViewController to a UIScrollview within a UIViewController? - ios

I am trying to use this as a starting point to learn about collection view: Creating a UICollectionView programmatically
I have a viewDidLoad which has a UIScrollView created like so:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 64.0,
screenWidth, screenHeight)];
How do I add in the UICollectionViewController as the following below doesn't work:
ACollectionViewControllerr *vc = [[ACollectionViewController alloc] init];
[self.view addSubview:vc];
Do you need a UIScrollView to scroll down or does uicollectionview have that functionality already?

UICollectionView inherits from UIScrollView, so you don't need to add an UIScrollView. Just add the UICollectionView to the UIViewController directly.
If you use UICollectionViewController then you don't require UIViewController.
See the documentation of UICollectionView

You're trying to add a ViewController to a view, which will not work. Try the following instead:
ACollectionViewControllerr *vc = [[ACollectionViewController alloc] init];
[self.view addSubView:vc.view];
[vc didMoveToParentViewController];

Related

Pass Subview to subclassed UIView

I have a subclassed UIView with a special behavior.
It is a custom class of a ViewController xib view. It contains a subview, which is currently created programmatically.
My question is; how to pass a subview either from the xib or from one created programmatically in the viewController via initWithFrame or initWithCoder to my subclassed view? (Right now it simply acts through the view, not initiated at all in the VC.)
I would share code, but I don't know that it's necessary.
Use the initWithFrame method:
SubclassedView *view = [[SubclassedView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:view];
Be sure to import #import "SubclassedView.h"

UIScrollView scrollViewDidEndDecelerating not triggering

I have a UIScrollView, created programmatically, inside of a UIView. What do I need to do to ensure that I can use the delegate method scrollViewDidEndDecelerating?
Here's what I have set up, please assume that within the UIScrollView, that there are three UIImageViews. When the page first loads, I am looking at the center UIImageView and I can scroll once backwards or once forwards. The reason why I need this delegate method is because I intend to use it to calculate which UIImageView I am currently looking at.
ViewController.h
#interface ViewController : UIViewController <UIScrollViewDelegate>
ViewController.m
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.pagingEnabled = YES;
[self.view addSubview: scrollView];
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(#"scrollViewDidEndDecelerating");
}
Add
scrollView.delegate = self
after the scroll view initialization.
In your delegate method, you can test each uiimageview center to see Which one match the uiview center, That's the uiimageview you are looking at.

Subclass UIView from ViewController

I'm trying to show an onscreen tutorial (like a picture with hints) in my viewController. I only know how to "open" a UIView with its drawRect method, where my paint code is inside, from the AppDelegate with:
BannerView *view = [[BannerView alloc] initWithFrame:self.window.frame];
[self.window addSubview:view];
[self.window makeKeyAndVisible];
Is it possible to activate the UIView (BannerView) by a button from inside a ViewController?
Thank you very much!
Simply use this code:
BannerView *view = [[BannerView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:view];
in your button action method (if it is defined in your controller class). See this S.O. question to know how to create a button programmatically. Otherwise, you can use interface builder for that.
Using self.view.bounds in initWithFrame will make your banner view as large as the controller's view (which could be smaller than the display).
I believe what you are looking for is adding an event handler to a button on the view controller that will create the view and add the view by using something like
BannerView *view = [[BannerView alloc] initWithFrame:CGRectMake(x,y,width,height)];
[self.view addSubview:view];
where self is the view controller.

Can't add UITableView as subview

Xcode 4.3/iOS 5.1/Storyboards.
I have a UIViewController with a UITableView in the storyboard
when initializing that view
-(void) loadView {
UITableView *tTableView = [[UITableView alloc] initWithFrame:CGRectMake(20,60, 300, 300)
style:UITableViewStyleGrouped];
[tTableView setDelegate:self];
[tTableView setDataSource:self];
[tTableView setBackgroundView:[[UIView alloc] initWithFrame:self.tableView.bounds]];
self.tableView = tTableView;
self.view = tTableView; // <--- THIS WORKS
}
This works, but the TableView is stretched over the entire width of the screen. I'd like it to be of a certain size as I'm using it for a login username/password control.
I was told to add it as a sub-view, but this crashes my app with some kind of recursive calling into the same "loadView" method.
-(void) loadView {
UITableView *tTableView = [[UITableView alloc] initWithFrame:CGRectMake(20,60, 300, 300)
style:UITableViewStyleGrouped];
[tTableView setDelegate:self];
[tTableView setDataSource:self];
[tTableView setBackgroundView:[[UIView alloc] initWithFrame:self.tableView.bounds]];
self.tableView = tTableView;
[self.view. addSubview: tTableView]; // <-- THIS CRASHES
}
Why is this crashing? How can I add the UITableView to a subview where I can control its width and not have it occupy the entire width of my screen?
you need to self.view = tTableView or self.view = [UIView alloc] initWithFrame.... and then add the table view. Overriding the loadView method means you are going to create the self.view. So for your case, create an UIView, set it to self.view and then add your tableView as subview of self.view
You use the loadView method, which means you define the view by yourself.
So self.view is not defined and the controller complains that its view is not defined when it should display itself.
Either put your interface in a xib (or storyboard) and bind those values in viewDidLoad, or instanciated the self.view by hand in loadView.

How to addSubview with a position?

I have something like this:
myViewController = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
[mainCanvas addSubview: myViewController.view];
self.view = mainCanvas;
It will be added at the position (0, 0), but I want to add it at (0, 100) or somewhere else. How can I do so?
Something like this:
myViewController = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
myViewController.view.frame = CGRectMake(0, 100, myViewController.view.frame.size.width, myViewController.view.frame.size.height);
[mainCanvas addSubview: myViewController.view];
self.view = mainCanvas;
In addition to setting the frame property, you can also set the center property of a view.
Set the frame property on the sub view.
This is the best way I've found to add a subView, like a loading screen or something that you want to show whether you are in a UIView, UIScrollView, or UITableView.
myViewController = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
myViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:myViewController.view];
self.view.bounds = myViewController.view.bounds;
This will make the subView appear in full screen no matter where you are in the self.view by adding the subView to where you are currently located in your self.view instead of positioning it in the top left corner, only showing fully if you are at the very top of your view.

Resources