How to use UIScrollView in IB? [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
steps for creating UIScrollView with Interface Builder
I am using UIScrollView for one of my view. I have added view first and then scroll view, then some controls over scroll view, all are added through IB. Every things works fine until the page is fit, some controls need to be added down that is exceeding the UIView length, I am not sure, how to add controls in IB in that case, So I have added programmatically, but my scrollview is not scrolling down, it is scrolling upto my view height, I have tried setting the new view height, scrollview's frame and content size nothing works, Can anyone please suggest me what to do now...
i) how to add control in IB more than its view length.(view height are disabled).
ii) How to make my scrollview which covers full view.

Create the view for the UIScrollView separately.
Technically, UIScrollView can hold controls directly in it. But that is not the point of it. You should always (if using IB) design the view separately and then place it inside the UIScrollView and set the contentSize property to the size of that view.
So, instead of putting contents directly in the UIScrollView in the main view, drag a separate view from the IB objects library on the nib (not on the main view).
Set the width of this view to the width of UIScrollView. Start placing controls on it and extend the height of view. For every new control to be placed, the height must be increased so as to hold that control. You should not place control outside a view from IB. (Although you can).
After designing the view, drag the view inside the UIScrollView and set the Y value of this view to 0. (When you drag a view inside UIScrollView it seems to upset it's Y value).
Now programmatically set the contentSize of UIScrollView to the size of this view. For this you'll need to create an IBOutlet for the UIView you just created.
myscrollview.contentSize = myview.frame.size;
And you're done.
-EDIT-

it is so simple. Follow the steps I'm giving you below you will get what you want.
decide the actual size that is the size in which you want your scroll view to display. say it is 100 * 5o.
set your UIView size also 100 * 50.
Now increase the scrollview hieght in nib.
add what you want
Now check upto what size you have reached.
Suppose it is reached upto 300
So set your contentsize 100 * 300 or 100 * 310 programmatically
In nib set scrollview size 100 * 50
Enjoy programming.

It's very simple, first remember your UIScrollView Frame and then just change UIScrollView frame's Y-position by subtracting 100 (e.g 0 to -100) and add same to height(460 to 560). Then simply add you controls at your required position by continuing this steps...
For eg:
ScrollView Frame (0,0,320,460)
after CHANGE:
(0,-100,320,560)
After finishing with controls adding simply set previous frame like:
(0,0,320,460)

Related

UITableView with dynamic height inside UIScrollView:

I have a screen with UINavigationController and UITabBar.
The UINavigationController screen is a UIView (in figure below, in red), that haves an image in the top and a UIScrollView (in figure below, in green). The UIScrollView have two UILabel ("Some text") and a UITableView that DO NOT ENABLE SCROLLING.
Every time the list become bigger, i need to enlarge the tableview height, dynamically. At the same time, I think I need to enlarge the UIScrollView. See the image below:
How can I define the dynamical height to UITableView and UIScrollView?
My goal is: Every time the list become bigger, the UIScrollView become bigger too.
My environment:
My UITableView DO NOT ENABLE SCROLLING
I am using XCode 4.6 and the view is a FreeForm
My view is using AutoLayout
I tried to do this setting just the UItableview height, without success.
How can I do this?
You can calculate the height of table view bin the method
heightForCellAtIndexPath:
in this method you pass the height of each row in this you can calculate height by creating a globale variable and in this variable add the row height each time.
or you can find it by having no of rows * height of a row in height of a row is constant.
may this will help you.

iPhone UIScrollView how to properly zoom the view? Currently it's height is not adjusted properly

I have a UITableView within a UIScrollView. It took me quite a lot of work to make it work.
The tableView is 640x350, I use the scroll view to scroll from one end of the cell to the next.
The scroll view is 320x350.
The scroll view's content size is 640x350
I'm running into this problem:
if I set scrollView's minimum zoom scale to 0.5, the tableview's width now fills the screen, but it's height is only half the screen. I would like the tableview to show more rows when I zoom out to 0.5.
First of all I would like to understand if this is the correct behavior, or the result of my tableView's content size and frame manipulations. The tableview has all springs and struts set in interface builder and should fill the frame available. This is my first attempt at zooming in months, and I don't remember how it works with zooming.
Can someone help me understand where and what do I need to adjust?
As far as I understand, I need to put the code into scrollViewDidZoom: that will manipulate the tableView's frame and content size.
PS. I"m returning the tableview from the viewForZooming: method of UIScrollView
What you are trying to achieve is pretty hard.
Solution 1 This solution uses the exact setup you have (UITableView inside UIScrollView).
You say that when you set the zoomScale to 0.5, you want your table view to fill the scrollView vertically. At 0.5, your table view must be 640x700 in order to fill the UIScrollView as you wish. For this to happen, on scrollViewDidZoom: you must resize the frame of the table view to 640x700
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
// No matter what the zoomScale is, the tableView will be zoomed accordingly
// Only zoom the height of the table
tableView.frame = CGRectMake(0, 0, 640, 350 / zoomScale);
// Also, update the contentSize
scrollView.contentSize = CGSizeMake(640, 350 / zoomScale);
}
If you run the code above for zoomScale = 0.5 you will get a frame size of 640x700.
This only changes the frame of the table and doesn't change the heights of the cells. This means that as you zoom out, you will also see more cells in the tableview.
Solution 2 Use only UITableView
UITableView is a subclass of UIScrollView. This means it has the ability to zoom and scroll around.
Start with a UITableView with the size that you want on the screen. Then, after the content is loaded modify the contentSize and make it wider than your frame width. This should enable horizontal scrolling.
However, UITableViewCells have their frame set automatically to the width of the tableview frame. You can bypass this by using a custom UITableViewCell, with clipsToBounds=false. Inside it you will insert a UIView with the frame set to the width&height you desire and with no autoresizingMask. When the tableview will resize UITableViewCell frame, this will not affect your inner UIView.

Dynamically set UIView size depending on height of grouped table view

Setup: I have a UIView with a scroll view nested within it. Within the scroll view I have a label, uiimage, and a tableview (grouped). The label, uiimage, and tableveiw are populated by a webservice. The last section in the grouped table view contains text that will never be the same and could be as long as 5 characters to 250+. Also, this view is pushed from a basic tableview (so it has a navigation bar. If that matters at all).
Expected: The uiview should extend in height depending on the height tableview extends to. Then I will be able to set the scrollview to accommodate the height I need to be able to scroll.
Problem: I'm not quite sure how to approach the issue. I really only know how to change the height to fixed values, which will not work properly in almost any scenario.
Am I using UIScrollView incorrectly? Do I need to resize the UIView at all?
You don't have to modify your UIView frame size, which has to be the size of your screen. The UIScrollView frame size must also be the same, it represents the part of its view actually displayed.
What must change is the UIScrollView contentSize, which defines height and width for data inside it ;)
You can calculate it using each inside element's height and by adding the correct margin.
Thus, you could have a UIScrollView content size of 320 * 600, which will let you automatically scroll down.
In fact, you have to display your content independently of the final frame size. If you have a content of 500*500, just display it inside your UIScrollView. Then tell it the size of it's content, and it will automatically set scrolling possibilities if needed.
Turns out I had to create a UIView programmatically and set it as the header of the UITableView. It works perfectly now. :)

UIScrollView won't scroll!

In IB I have my UIView. Then I have a sub-UIView with a UIScrollView as a sub view. Then the UIScrollView has a sub-UIImageView. The UIScrollView and UIImageView are the same size. They're much bigger than the UIView of which they are subviews. I assumed this would make scrolling work. It doesn't. Is some sort of code required for scroll views to work?
You need to set UIScrollView.contentSize to match the total scrollable size, which is your subview frame size in this case.
As mentioned in the accepted answer, you must set the UIScrollView's contentSize property.
This can be done in Interface Builder.
Select the scroll view
Select the 'identity inspect' in Utilities pane on the right
Under 'User Defined Runtime Attributes' click the '+' button
Set the 'Key Path' value to 'contentSize'
Set the 'Type' value to 'Size'
Set the 'Value' value to '{width, height}' (eg: '{320, 600}')
Build and run and your scroll view will scroll.
The content inset does not affect scrolling. See What's the UIScrollView contentInset property for?
To scroll, you have to make the scrollview's frame smaller than its content, the contained image or view.
This might be obvious to most, but I spent ages wondering why my UIScrollView wouldn't scroll so I'm posting what was stopping me in case it helps anyone else:
The UIScrollView has to be of the dimensions of the visible area in which you wish it to be presented and not the size of it's contents.
Ridiculous on my behalf I know, but just in case it helps someone.
I placed all the content of my scrollview in IB. (buttons, labels, text fields, etc). The full size is 500 tall.
I then resized it to 436 tall in IB.
Then in code, I put this is viewDidLoad:
optionsScrollView.contentSize = CGSizeMake(320,500);
So that leaves 64 pixels that I can scroll. It works perfectly.
I also placed "UIScrollViewDelegate" in the <> braces of #interface for my .h file and tied the delegate outlet of the scrollview to File's owner in IB.
I could solve the scrolling problem with the following answer:
https://stackoverflow.com/a/39945124/5056173
By me the trick was:
You now need to set the height of the content UIView. You can then either specify the height of the content view (blech) or use the
height of the controls contained within by making sure the bottom
control is constrained to the bottom of the content view.
I have set the height and width of the view inside the scrollView with center vertical and horizontal alignment and that was the reason, why it did not work!
After deleting this constraints, I need to add equal width (scrollView and the view inside the scrollView) AND I set the height of the view inside the scrollView directly with the content. Which means: The last element in the view must have a bottom constraint to the view!!
The other important thing that I don't see mentioned here is that UIScrollView does not play nicely with AutoLayout. If it seems like you've done everything correctly, check if your ViewController has autolayout turned on and, if so, turn it off.
(Every time you scroll, the views are re-laid-out. Gak!)
So:
Make sure scrollview's contentSize is bigger than its frame.size
Make sure AutoLayout for the ViewController is turned off.
more, did you enable scrolling?
look at the property
#property(nonatomic, getter=isScrollEnabled) BOOL scrollEnabled
Make sure 3 things,
checking scrollView frame & contentView frame, u may find the answer
scrollView.isScrollEnabled = true
contentView of scrollView height didn't constraint with scroll view height
UIScrollView won't scroll!
reason: contentSize is same as (sub) view
should: contentSize is large than (sub) view
-> UIScrollView can scroll
how set UIScrollView contentSize?
two method:
in code
- (void)viewDidLoad {
[super viewDidLoad];
。。。
//[(UIScrollView *)self.view setContentSize:CGSizeMake(375, 1000)];
CGSize curScreenSize = UIScreen.mainScreen.bounds.size;
CGFloat scrollWidth = curScreenSize.width;
CGFloat scrollHeight = curScreenSize.height * 2;
[(UIScrollView *)self.view setContentSize:CGSizeMake(scrollWidth, scrollHeight)];
in UI (Storyboard)
Storyboard-》Identity Inspector-》User Defined Runtime Attributes-》 add new attribute:
contentSize
Type:Size
Value:{375, 1000}
Scroll view works with this:
Frame
then
contentSize
views or objects etc...
If your frame is set to your content size then it won't scroll.
So set your frame ( in IB right panel -> second last tab 'Size Inspector") to the length of your app ( in my case it is 367 as i have a navbar and a tab bar) then programatically set the contentSize to - yup you guessed it ... more than your frame so it can scroll.
Happy days!!

How should child views of UIScrollView report their bounds for contentSize?

I'm looking more for advice on the correct design for a view.
What I have is a UIScrollView that contains one or more custom Views I have created. My problem is, who reports to the scrollview what it's contentSize should be? I have the following:
UIView
+-UIScrollView
+-CustomView 1 with dynamic height depending on data
+-CustomView 2 with dynamic Height depending on data
The UIViewController creates new instances of the custom views with data and then adds them as subviews to the UIScrollView. The problem I'm having is how to set the value of the scrollview's contentSize? Right now, I'm not doing that and the contents of the scrollview are clipped with no scrolling possible.
Should the custom view call [parent setContentSize:] in its drawRect:?
Should the UIViewController query the custom view after creation to get its bounds and then call setContentSize?
Should I subclass the UIScrollView to override addSubView to query each subview's height?
Is there something else I'm missing?
I hope I explained that properly. I'm new to this and still getting a handle on things.
The contentSize of the scroll view should be the size of the union of the frames of all your custom views. Whenever the size of a custom view changes, or one is added or removed, the view controller should calculate the new contentSize and apply it.
Setting it from drawRect: could essentially set up an infinite loop.
Using the bounds does not give the coordinates within the parent view.
You could subclass UIScrollView if the custom views do not change size.