UIScrollView how to set ContentInset and ScrollerInset - ios

im not able to configure my UIScrollView.
The image above shows my view. The orange box is my UIScrollView and the blue boxes (plus the orange box) is the view that i want to scroll.
The View is 520px with and the UIScollView (as usual) 320px.
How have I to set the ContentInset and ScollerInset?

Since the heights of the scroll view and its content match, and because it does not appear from your picture that you would like to add an empty space to the content, you should keep the contentInset set to zero (as it is by default). If you would like the content's middle to be visible through the scroll view, set content offset to (520-320)/2 = 100, like this:
[scrollView setContentOffset:CGPointMake(100, 0) animated:YES];
// If you do not want to see the view scroll, change this ^^^ to NO

I'm not sure exactly what your question is, but I'm guessing your scrollview is not scrolling as expected.
You need to set the contentSize property to tell the scrollview that there is more content than just the viewable area (e.g. myScrollView.contentSize = CGSizeMake(520.0, 100.0);)
If you want to start with the content centred as per your diagram, you can to set the contentOffset property. (e.g. myScrollView.contentOffset = CGPointMake((myScrollView.contentSize.width - myScrollView.frame.size.width) / 2, 0.0);)

Related

Resize ScrollView based on TableView size

I'm trying to create layout that it structured like this:
- View
-- ScrollView
--- ContentView
---- CustomView
---- CustomView
---- TableView
---- CustomView
The tableView itself is auto-resizable using "invalidateIntrinsicContentSize" and when I add items - the height of the tableview changes, pushing the custom view below it further down.
Once enough items are added I the bottom custom view is hidden and the scroll doesn't work.
important fact - the bottom custom view doesn't have a bottom constraint. It is pushed down by the it's top constraint to the tableView.
If I do set a bottom constraint - the table view will no longer be dynamically resized.
The intended behaviour:
When a user adds items to the list and the list gets too big the ContentView will be scrollable so the user can scroll to see the bottom view.
The actual behaviour:
When a user adds items to the list and the list gets too big, the bottom view is pushed down and outside of sight and content is not scrollable.
What is happening and how can I fix it?
Below is what I think what is happening.
Since you are using UITableView, it has its own scroll view. So when the UITableView list gets too big, UITableView itself becomes scrollable rather than ScrollView's contentView becoming scrollable.
To achieve what you need, you would have to make the UITableView not scrollable and use the intrinsicHeight of the UITableView to get the actual height of UITableView along with all the items. If you have items with varying heights, it will be a problem because you won't know the height before rendering. With same height for all the rows, you can get the total height of the UITableView and set the height constraint to that value. This will increase the contentSize of the outer ScrollView, making it scrollable.
Apart from UITableView, you can also use UIStackView. This is because you are not using the reusing capabilities of UITableView anyways. Managing the datasource and delegates should not be a big problem.
You can create a constraint for tableview height, And take its reference to your swift file, by dragging it as you take other views. Now in your code, Just do this
tableViewHeightConstraint.constant = tableViewNoOfItems * tableViewCellHeight;
if you have set other constraints perfectly inside scrollview, It should work perfectly. Means TableView should have top, bottom, left, right margined constraints from the ScrollView.
try this code
tblViewHeight.constant = CGFloat( tableview row count * 45 )
var size = contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
if size.height < scrollView.frame.size.height
{
size = scrollView.frame.size
}
contenViewHeight.constant = size.height - scrollView.frame.size.height
scrollView.contentSize.height = contenViewHeight.constant
What I think you could do is:
Disable tableView's scroll tableView.isScrollEnabled = false
Every time a user adds items to the list, reload the tableView
Also using UIStackView with vertical axis and .fillEqually distribution as a Content View would be much more convenient as you won't need to set any positional constraints to your views, but may need to set height constraints if intrinsic content size can't be determined by the engine

UIScrollView Bounces To Either Top Or Bottom

I have a UIScrollView for which I have a UIView which is the subview of the scroll view , the UIView has a lot of other subviews and I am getting the height for it dynamically after adding the subviews , this is my piece of code to add the view to scroll view
CGRect frameOfView = CGRectMake(0, 0,Get_Bounds.width, globalYPosition);
self.parentProductDetailView = [[UIView alloc]initWithFrame:frameOfView];
I am first initialising the view this way and then after adding all subviews I am doing this,
frameOfView.size.height = globalYPosition;
[self.parentProductDetailView layoutSubviews];
self.parentProductDetailView.frame = frameOfView;
[self.productDetailScrollView addSubview:self.parentProductDetailView];
self.productDetailScrollView.contentSize = CGSizeMake(0, self.parentProductDetailView.frame.size.height *1);
But my scrollview does not scroll properly it either sticks to top or bottom.
Here globalYPosition is the sum of height of all subviews added to parentProductDetailView
The procedure you used seems correct. No matter the subviews your scroll view should scroll properly by simply using a larger content size then its frame size.
The scroll view may stick, snap to some points if paging is enabled which is what is happening in your case. If the content view is larger then 1.5th of the frame size then the scroll view will snap to top/bottom or left/right. If it is smaller then it will only snap to starting position.
This may be very useful for situations like having a side menu that takes a part of a screen but in your case you should simply disable the paging and scrolling should work fine.

How to get a title to scroll above an iOS text view

I want to have a text title scroll above a text view, but can't find information on how to do it. I've been looking for days, but can't even figure out how to look for the info. I want the function to work like the Apple Notes app where the date text is positioned above the text entry location and scrolls off the screen with the text but is not editable.
I've tried placing labels above UITextView, but the label does not scroll with the textView. I have a sample Xcode project I'm work with, but not sure if it can be uploaded for others to see what I'm doing. I almost had success with the project, but the labels only scroll with the text in the landscape view, not the portrait view for some reason.
I've read Apple developer docs on TextViews and several other sources without finding any discussion on how to do this or examples to follow.
Can anyone point me in the right direction?
I'm sure there must be a better way to do this, but this is the only approach I've found to work after more than a week of trying to find a solution. If I can figure out how to upload the entire Xcode example project I will do that.
This is the explanation of the approach I found to work under iOS 6.1 and Xcode 4.5. I have only tired this on the iPhone 6.1 simulator.
After creating a full screen text view and placing a label at the top of the text view,
To get the label to scroll off screen with the editable text view you need to:
turn off autolayout in interface builder (uncheck Use Autolayout). (there appears to be a problem with autoLayout and scrollViews in iOS 6.x)
turn off the text view scrolling in interface builder (uncheck Scrolling Enabled)
set the text view content insets height (Top) to the label height
embed the text view and label in a scroll view.
set properties for the scroll view and text view to be able to access their properties
in viewDidAppear:animated:
a. set the scroll view content size height to 20 plus the greater of the screen view height or the text view content height plus the label height
b. set the text view delegate to self
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
float screenHeight = self.view.bounds.size.height;
// set the scroll view content height to 20 plus the greater of the view height or the text view content size height plus the label height
screenHeight = MAX(screenHeight, self.textView.contentSize.height + 20);
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, 20 + screenHeight);
// set the text view delegate
self.textView.delegate = self;
}
set the text view height dynamically in textView delegate method textViewDidChange:
a. if the text view content height is greater than the view bounds height less the label height:
1. set the text view frame to the view bounds width and the text view content height plus the lable height
2. set the scroll view content height to the view bounds width and the text view bounds height plus the label height
// dynamically set the text view content size height
-(void) textViewDidChange:(UITextView *)textView {
if (self.textView.contentSize.height > self.view.bounds.size.height - 20) {
self.textView.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.textView.contentSize.height + 20);
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.textView.bounds.size.height + 20);
}
}
set the struts and springs in interface builder
1. set the scroll view and text view struts for left, right and bottom
2. set the scroll view and text view springs for width and height
3. set the label struts for left, right and top
4. set the label spring for width
The same settings need to be made every time the view changes orientations.
There might be a way to do this by setting constraints in code under autolayout, but I barely understand constraints, much less setting them successfully in code.
Hope this helps someone else.
What you are looking for is called UIScrollView. Take a look at the Apple documentation here.
A good tutorial on UIScrollView can be found on the Ray Wenderlich site.

problems with scrollview, not scrolling full length

I have a scroll view, added in the IB, and also I added a lot of objects/controls to it on IB itself. Some of them are added to the scrollview but they are out of its bounds.
Back in the class, I'm setting its content size like this
self.scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height + 500);
The scrolling length I observe is not that much. I just observe like 500 px (while the height I set in the content size is around 900 px).
Please tell me what I'm doing wrong.
I use the IB to set the scroll view and the inner controls and I let the scroll view to be longer than the view area it self as why do you need to set it inside the code unless you draw the inner controls dynamically at the runtime ...I hope it is useful

Scrolling Issues in IOS

Scroll view did not set content size if I set it using a dynamic variable i.e.
scrollView.contentSize=CGSizeMake(320,scrollView.frame.origin.y+120);
the scroll view size remains same after calling above method.
But if I set it using a number (integer or float) it gets changed but it gives a very strange effect on scrolling. Picture of effect is attached.
scrollView.contentSize=CGSizeMake(320,1000);
This is not allowing me to post image anyways a strange effect appears.
How I can get rid of it?
Please tell Whats going wrong or what I am missing?
Now i have got rannking upto 11 the image for effect is given here.
Before Increasing Content Size:
After Increasing Content Size on Scrolling follwing effect appears:
Select the Scroll View and view its Size Inspector window. Observe that its size is 320 , 713 points (in my case). If you do not see the same
size as what I have, this is a good time to adjust the size so that it is the same as mine. You will
need to use this value in your code
(void)viewDidLoad {
scrollView.frame = CGRectMake(0, 0, 320, 460);
[scrollView setContentSize:CGSizeMake(320, 713)];
[super viewDidLoad];
}
i think you should use frame.height to achieve your goal
the content size must be greater then frame size of scroll view for scroll
and you are using y origin of scroll view so may be that's the problem.
scrollView.contentSize=CGSizeMake(320,scrollView.frame.size.height+120);
In this line:
scrollView.contentSize=CGSizeMake(320,scrollView.frame.origin.y+120);
You are setting the scroll view's contentsize's height to scrollView.frame.origin.y+120.
Means, if your scroll view's frame is x=40, y=40, width = 500, height = 500. Then the scrollView.frame.origin.y+120 will be equal to 40 + 120 = 160. Which is less than your scrollview's height. So your scroll view won't scroll to up or down.
In the second line:
scrollView.contentSize=CGSizeMake(320,1000);
You are scroll view's content size's height to 1000. That is greater than the scroll view's height so it'll scroll to up and down.
Finally.......
The issue was scroll view increases width of scroll view indicator which shows on the screnn while scrolling. You have two options to get rid of this.
Disable vertical or horizotnal indicators which one causing problem, but this may cause some problem with scroll view content size in my case it caused a problem last cell was not being viewed.
2.Let it appear but make it out of screen by the following code snippet.
[myScrollView setScrollIndicatorInsets:UIEdgeInsetsMake(0.0, 0.0, 0, 320.0)];
this is working perfectly but scroll indicator is gone out of screen.

Resources