I'm currently trying to center a UIScrollView inside of my UIViewController class and am having a problem.
The UIViewController is inside of a tab bar and a nav bar, so it is 367 pixels tall on iPhone and 320 pixels wide.
The UIScrollView is placed inside of that UIViewController, and the UIScrollView is being loaded from another UIViewController class responsible for displaying content.
Now, my goal is to make the scrollview 10 pixels less in width than the containing UIViewController so that the scroll view does not take up the width of the screen.
However, in Interface Builder, when I select 300 as my UIScrollView width, and in my implementation file for the containing UIViewController, when I set the width as 300,
[scrollView setContentSize:CGSizeMake(300, 1500)];
the end result is a full-screen scroll view instead of one cut off by 5 at each side.
Any ideas on how to take care of this?
Thanks.
You can use uiscrollview's
#property(nonatomic) UIEdgeInsets contentInset
However, it is possible that when you are using in the class1 the scroll view from the class2, scroll view frame/content size are read from class2 object instead of class1 nib.
Related
I've added a scrollview to a view in my app, the view is selected from a tab controller and as such is a container and not a UIViewController it is declared as below
class HomeViewController: Container, UIScrollViewDelegate {..
I've added the scrollview in the storyboard and added all of the components to it and I've assigned the delegate from the storyboard and I've placed this code in my ViewController
scrollview.contentSize = CGSize(width:1048, height:scrollview.frame.height)
scrollview.delegate = self
This how my ScrollView looks in my storyboard, you can see that scrollview is my UIScrollView, then I've added a UIView viewScroll and then added four views to viewScroll (View1, View2, View3 and View4), scrollview is sized at 375x340 and viewScroll is set at 1048x340
When I run the app it does not scroll. As it is not a UIViewController, do I need to approach this differently or is there something else I have missed?
I've added the scrollview in the storyboard and set the width as 1050 and height as 330
You should set the width and height of scroll view to be at most as large as its containing view. What you need to set to 1050 and 330 is contentSize of the scroll view - scrollView is only scrollable when its contentSize is larger than its bounds.
If you're setting up your scroll view using Interface Builder, then you'll need to add constraints to its children's edges. For example, in your case you'll have to add leading, trailing, top, and bottom constraints to your View Scroll view. Select it and add following constraints in interface builder:
After that there will be something like this:
When you add edge constraints to UIScrollViews child views, you are hinting the scroll view about its content size. And if scroll view knows its content size and it is larger than scroll view's bounds, you'll have scroll working.
I added a UIViewController, then in the XIB i dragged a UIScrollView, and a few buttons at the bottom of the page. Then when i build and ran the application, the application scrolls but the button that i added at the extreme bottom of the page can't be seen properly. So i think i will have to set a height for the scrollview. But where in Interfacebuilder i should specify the height ?
If it can't be done in Interfacebuilder, then can someone show me how to do it programatically ?
You need to set the contentSize property of the scrollview. You cannot do it in Interface Builder. You need to set it in the code of the view controller that manages the scroll view.
scrollView.contentSize = CGSizeMake(width, height);
I'm creating a custom subclass of a UIViewController (without a nib), which I'm pushing onto a UINavigationController stack. Somewhere during the initialization of my UIViewController subclass (loadView? viewDidLoad? init?) I want to add a UITabBar subview to the bottom of the view. The problem is figuring out the Y-coordinate. As far as I can tell, the view gets resized somewhere after loadView, viewDidLoad, and init so I can't get the resized height in order to calculate the Y-coordinate of the UITabBar.
What is the proper way to figure out the height of the containing view such that I can anchor the UITabBar at the bottom?
What you need to do is set your UITabBar to the bottom of the view, and then tell it to stay there if the bounds of the superview changes.
This isn't too tricky. For example, in viewDidLoad alloc/init the tab-bar as normal, and position it as follows:
tabBar.frame = CGRectMake(self.view.frame.size.height-tabBar.frame.size.height, 0,
tabBar.frame.size.height, tabBar.frame.size.height);
[self.view addSubview:tabBar];
...which will add the bar to the bottom of the view.
So far so good: your problem is when the superview changes height the tabbar doesn't stay locked to the bottom. To fix this, we set an autoresizing mask:
tabBar.autoresizingMask = UIViewAutoresizingMaskTopMargin;
...which will effectively lock the bar to the bottom of the superview.
I have created an iPhone UI programmatically but I just can't figure out how to fit a view so that it doesn't get overlapped by the TabBar. Here's the ownership hierarchy:
- AppDelegate
UITabBarController
UINavigationController x
UITableViewController
UIViewController (with XIB)
UIScrollViewController (in the same XIB as parent)
The problem is that my TabBar hides a part of my UIScrollView (and my tableviews as well, but that's not important). I tried manipulating the scrollview content size and the view frame size but with no luck.
So far the best I could do was to add a 60px padding on the scrollview's content size like this:
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+60)];
Thanks for your help!
Sounds like you may have to make sure your scrollView's frame is set properly first. The contentSize will determine the dimensions of the content of the scrollView. In other words how far you can scroll in each direction. Take a look at the scrollView in your nib and make sure the frame is the right size you need to fill.
When I push a UIViewController within a UINavigationController, the UIScrollView in the view changes dimensions. Now even I am trying to resize the UIScrollView. It wont simply resize. It maintains the framework dimensions. Any idea why this would be happening.
Example:
My NIB has a view which has a scrollview with dimensions (320, 430)
now when I push the UIViewController associated with the view the scrollview still shows dimensions has 320, 430, but is drawn much smaller then 320,430 alt text http://img717.imageshack.us/img717/6554/screenve.png
Any suggestions in this regard.
This was happening because of the autosizing mask property of the view i created programmatically was set. Resetting the flag solved the problem.