Scroll view not working in drag and drop approach. why? - ios

If we add scroll view to storyboard it will visible on VC, but it not scrolled Why? Can any one please tell me the answer......

To make it work you need to do following.
Set the delegate property of your UIScrollView.
Set its contentSize.
Make your controller to follow the delegate of UIScrollView by adding <UIScrollViewDelegate>. in your class interface declaration.
I hope you have created your UIScrollView outlet then add this code in your viewDidLoad method.
self.myScrollView.delegate = self;
[self.myScrollView setContentSize:CGSizeMake(WIDTH, HEIGHT)];

Related

xib using autolayout not showing when added as subview

I had an UIViewControllerA created with xib for view reusability.
In the xib I simply added another red view to it and set its constraints to top,leading,trailing, and bottom space to red view's superview (which is my A's view).
Add A to another UIViewController B as B's ChildViewController by doing so:
// UIViewControllerB.m
UIViewControllerA *A = [UIViewControllerA alloc] initWithNibName:#"UIViewControllerA" bundle: nil]];
[A.view setFrame:CGRectMake(0,0, self.view.size.width, self.view.size.height/2)];
[self addChildViewController:A];
[self.view addSubview:A.view];
[self.A didMoveToParentViewController:self];
Nothing red or whatever showed up.
I did try [self.A.view setTranslatesAutoresizingMaskIntoConstraints:NO]; though
that seemed to make my constraints useless and actually did not bring up the red view.
Do I have to code the whole thing (UI elements & constraints creation) when I'm working with xib and need some extra view with autolayout?
Thanks for any advice!
The problem is this line:
[A.view setFrame:CGRectMake(0,0, self.view.size.width, self.view.size.height/2)];
Instead, give this view constraints.
When you insert a set of views that uses autolayout into a set of views that uses autolayout, you must use autolayout to position it.

Custom view not resizing with auto-layout

Note: You don't have to check up the library to understand the question, I just brought it up for reference.
I implemented DIDatePicker in xcode. The way I inserted it in a view is: I inserted a UIView to the viewController with auto-layout, then at the Identity inspector I set the class to DIDatepicker.
The problem is, DIDatepicker doesn't resize to the view's size. I was thinking a possible solution may be, to set DIDatePicker as the view's class in viewDidLoad, though I don't know how to do that.
My question is: if my theory is right, how can I set DIDatePicker as the view's class. If what I said is not true, how can I make DIDatePicker to resize with the view?
DIDatePicker has a collectionView; here is how it's defined:
UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
[collectionViewLayout setItemSize:CGSizeMake(kDIDatepickerItemWidth, CGRectGetHeight(self.bounds))];
[collectionViewLayout setSectionInset:UIEdgeInsetsMake(0, kDIDatepickerSpaceBetweenItems, 0, kDIDatepickerSpaceBetweenItems)];
[collectionViewLayout setMinimumLineSpacing:kDIDatepickerSpaceBetweenItems];
_datesCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:collectionViewLayout];
so I had the same exact problem as you.
The problem is that when you build DIDatepicker it builds its own collection view based on the view's original frame rather than the frame after auto-layout takes place.
The way to circumvent this problem is by not making the storyboard UIView of class DIDatepicker. Instead, just use a normal UIView and then create a DIDatepicker instance with the same frame as the UIView in the ViewDidLoad Method.
Here is an example. datePickerView is the UIView from the storyboard.
datepicker = DIDatepicker(frame: CGRectMake(datePickerView.frame.origin.x, datePickerView.frame.origin.y , self.view.frame.width, datePickerView.frame.height));
self.view.addSubview(datepicker!);
If that doesn't work try changing the frame and adding it to the datePickerView as a subview instead of the main view.
Hope this helps!
I'm not totally sure if your "theory" about setting the main view to DIDatepicker is correct, but you should be able to test it by selecting the default "View" that is in the View Controller and setting a Custom Class on that. You shouldn't need to add a UIView first, just use the one that comes with the View Controller.

Add clickable and fixed subview to UITableViewController?

I'd like to place an ADBannerView object onto my UITableView screen statically, what means that I want it to always stay above my toolbar (self.navigationController.toolbar), even when the user is scrolling the tableview. I've solved this by adding by ADBannerView as a subview to my toolbar and given it negative values for the frames origin:
[self setBannerViewSize];
[self.navigationController.toolbar addSubview:bannerView];
The only problem is: I can't click and open the iAd this way - I can see the banner but nothing happens when I tap on it.
Since I'm also using a refreshControl, the option to use a UIViewController instead of UITableViewController and add a tableView manually wouldn't work for me. Is there any other way I can get my ADBannerView statically showing in my table view controller AND still being tappable?
Thank you in advice!
Yay!! After all I succeeded in solving this (really annoying) problem by myself (and a lot of reading around)!
First, I found this really world-changing post. Basically this post handles with the topic that a UITableViewController uses self.view for its tableView property, so overriding the tableView property (or synthesizing it manually) plus giving self.view a new view (from application) and adding tableView as its subview would make it possible to reach the real superview of tableView.
But this still didn't solve my problem, although I was sure it would, because it all made sense. My bannerView appeared in the right place (and was fixed) but it still didn't do anything when clicked. But there was a second minor thing I didn't know about:
As I read in this post the superview of a subview doesn't only have to be userInteractionEnabled but also have a non-transparent backgroundColor. Because my superviews background color was set to [UIColor clearColor] it all didn't work - but setting its backGroundColor to e.g. blackColor solved the whole problem: the bannerView got finally tappable! :)
So, my code is now looking like this:
#synthesize tableView;
- (void)viewDidLoad
{
[super viewDidLoad];
if (!tableView && [self.view isKindOfClass:[UITableView class]]) {
tableView = (UITableView *)self.view;
}
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.tableView.frame = self.view.bounds;
[self.view addSubview:self.tableView];
[self resizeTableToFitBanner];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:bannerView];
// some other code
}
BannerViewController in Apple's iAdSuite sample code solves this problem very elegantly:
https://developer.apple.com/library/ios/samplecode/iAdSuite/Introduction/Intro.html
I think you should use a container view, and set things up in IB. You can add a tool bar and ADBannerView to the bottom of the view of your navigation controller's root view controller. Fill the rest of the space with a container view - this will give you an embedded view controller automatically. You should delete this one and then drag in a tableViewController and control drag from the container view to the tableViewController to hook up the embed segue.

Change UIView to UIScrollView

I built in my storyboard this "myView" view controller scene:
I have a simple view containing an ImageView and a UILabel (that I reference in code through IBOutlets).
What I want is transform this view to a scrollview and I was wondering if I could just adjust things in the storyboard for this purpose.
I also tried to change the view to scrollview programmatically adding to the viewController's "ViewDidLoad" method the following:
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
scrollView.contentSize=CGSizeMake(320,600);
self.view=scrollView;
But I'm not able to see my imageView and my label if I do So..Why?
I also tried (in storyboard) to assign the view (inside the "custom class" panel) the UIScrollView class but that's not working.
OK I found the solution, I changed the view type (identity inspector), but the view property of a UIViewController isn't a UIScrollView. So I get round this with casting the view to a UIScrollView:
[(UIScrollView *)self.view setContentSize:CGSizeMake(320, 1000)];
Add View to controller->Identity Inspector->Class-Change to UIScrollView
Resize your view
(self.urview as! UIScrollView).contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height)

adding subviews in uiviewcontroller make it goes will

I have a custom UIView that I need to add as a subview in a UIViewController.
But if I use [self.view addSubview:newView]; the app goes in a infinity loop and doesn't start. But if I use self.view = newView then it works. But I need it as a subview.
The UIView contains a grid layout of custom button.
I guess you do that in the loadView method. You must not call the view method of the view controller in loadView because that will itself call loadView! However calling setView: (e.g. self.view = newView) is okay.
My suggestion is to add the subview in viewDidLoad.

Resources