The UIScrollview always bounces back to top in IOS Objective C - ios

I have a scrollview which always bounces to top after I scroll to bottom. I have tried numerous ways to fix this. I know there has been asked same kind of question but none of that helped me.
These are the code I have used
[self.nfaSscrollview setContentOffset:CGPointMake(0,3185)];
also
[self.NFASscrollview setContentSize:CGSizeMake(self.NFASscrollview.frame.size.width,3185)];
This I have written in viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
[self.NFASscrollview setContentSize:CGSizeMake(self.NFASscrollview.frame.size.width,3185)];
_NFASscrollview.scrollEnabled=YES;
_View3.hidden=YES;
self.view1HeightConstraint.constant = 0.0;
[self.view layoutIfNeeded];
3185 is just the (height+origin of Y) of the last content inside my Scroll view.
Screenshot of constraints of view
I think there might some problem with my constraints. I have no clue how to fix this.

Check for negative value in constraints of your super view and scrollview and set it as 0. Even I faced the same issue I fixed it by setting the bottom space to constraint of my Super View to 0.

Before you get the frame of NFASscrollview, try using [self.view layoutIfNeeded]. Because this allows you to get the current accurate frame of NFASscrollview from the constraints of Autolayout.

Related

Adding ScrollView to ViewController with 2 custom Views

My viewController has one view with images and labels and one textView
Im new in objective c.
My problem is to add ScrollView in my ViewController with 2 custom views(UIView and UITextView).(image in the link) I have tried many things posted here in Stack but nothing works for me.
Thank YOU!
Here is what i have :
self.scrollView.contentSize=self.scrollView.frame.size;
self.scrollView.frame=self.view.frame;
[self.view addSubview:self.scrollView];
Adjusting view's frame was the technology of 5 years ago. You should never set the frame manually, not anymore. Instead start learning Autolayout and Constraints.
These tutorials may help:
https://www.raywenderlich.com/115440/auto-layout-tutorial-in-ios-9-part-1-getting-started-2
https://www.appcoda.com/auto-layout-guide/
You are setting the content size equal to the frame size before you actually set the frame, so it's probably just 0.
You need to just switch the calls around:
self.scrollView.frame=self.view.frame;
self.scrollView.contentSize=self.scrollView.frame.size;
[self.view addSubview:self.scrollView];
The other thing to keep in mind is that because you are setting the frame of a nested view to the frame of its superview, your layout will break (or at least not do what you expect), if the origin of your superview ever changes. If the origin is 0, 0, then you are fine for the moment, but otherwise you may want to set the subview (scrollView) frame to be equal to the superview (self.view) bounds instead of the frame, like this:
self.scrollView.frame=self.view.bounds

Adding Scroll view using storyboard in ios

I am trying to add a scroll view using storyboards in iOS. I have done so and it is working, but the problem is when I am using a scroll view of content size more than 800 it is not working. I have gone through many tutorials and have found that it will work only when autolayout is unchecked. Can we make it work with autolayout selected? Can anyone help me with this? Thanks in advance.
In order to make a scrollview scrollable, the content size must be larger than the scrollview's frame so the scrollview has something to scroll to. Use setContentSize to adjust the content size:
[scrollview setContentSize:CGSizeMake(width, height)];
refer this link UIScrollView not scrolling in iOS7 with autolayout on
try this
add the method
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[yourScrollView setContentSize:CGSizeMake(SOME_FLOAT_VALUE, SOME_OTHER_FLOAT_VALUE)];
}
I continue to have difficulties with this also. This is what I have tried. It works well, but I do get a warning, maybe someone can comment and help us all out.
Keeping auto layout on I have done the following..
In my view controller I dropped in a scrollview and sized it to whatever size was needed, but either the size of the screen or smaller. I then set the following constraints with the pin and alignment tabs for the scrollview... Top to view, bottom to view, both sides to view, and center in container. This allows for rotation.
Then, I dropped in a view and sized it to be larger than the scrollview. I have extended both the length of the view and its width for different apps. Inside this view you put all of your subviews, (image views, buttons, whatever you need) to be contained in the scrollview. For this view I have also set the following constraints... Top to scroll, bottom to scroll, width, and center in container. Then in the left screen for the storyboard I selected the bottom constraint for the view. It should be some negative number. In the attributes inspector, I then made this bottom constraint 0.
This has worked for me in the past, I can scroll in both portrait and landscape. I have also added pinch gestures and pan gestures for the view to be able to move things around. Hope this helps.
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[yourScrollView setContentSize:CGSizeMake(SOME_FLOAT_VALUE, SOME_OTHER_FLOAT_VALUE)];
}
for ipad(IOS7)
SOME_FLOAT_VALUE=0;
SOME_OTHER_FLOAT_VALUE=1200;
This is enough for scrolling a view

UIScrollView using Autolayout will only bounce

I am really struggling to get a UIScrollView to work correctly with Autolayout. Instead of scrolling down as it should, it just bounces, so if I drag it to see more as soon as I let go it returns to its original position.
I have my scene set up in the following way:
-Main View
- Scroll View
- Content View
- Label
- Label
The View Controller has its size metric set to Freeform. The Main View, Scroll View and Content View all have their Height set to 700, so I can see the layout correctly.
In my .h file I have an outlet connected to the UIScrollView and in my viewDidLoad method I am doing the following:
Scroller.translatesAutoresizingMaskIntoConstraints = YES;
[self.Scroller setContentSize:CGSizeMake(320, 1000)];
I've tried setting all sorts of constraints, on various things. Normally when I add a new one Xcode then grumbles that the constraints are incorrect and prompts me to update them. I have tried so many different variations, I can't remember them all but here are a few:
Pinning the height of the Scroll View
Pinning the bottom of the Content View to the Scroll View - Getting UIScrollView to work with AutoLayout
Pinning the height of the Content View
Other things I have tried:
Reading & following Apple's Technote: - https://developer.apple.com/library/ios/technotes/tn2154/_index.html
Setting the ScrollView's Content Inset - Confusion Regarding UIScrollView and AutoLayout
Setting the Content Size: UIWebView's UIScrollView subview can scroll but bounce back to the frame without appearing vertical scroll indicator
I'm sure this must be relatively straight forward, but I have been tearing my hair out. There are many similar questions on Stackoverflow and Google, but none of them seem to fully meet my requirements.
Any help or resources would be greatly appreciated.
JA
One possible reason for this is that scroll view's contentSize is reset after layout event which happens after viewDidLoad (for example due to incorrect constrains settings).
I had to put the code in viewDidAppear for it to work right
CGSize size = CGSizeMake(320, 931);
self.scroller.scrollEnabled = YES;
self.scroller.contentSize = size;

Setting up UIScrollview and autolayout

I created a UIScrollview and added a few UIElements to it, it works without autolayout no problem, scrolls etc. However its good practice to use autolayout in iOS7. Now the scrollview does not scroll. How do I accomodate for this?
If I turn auto layout off, there is extra space added to the top of the uiscrollview in ios7.
I used to set up the scrollview by 'unchecking' autolayout and implementing the following code.
//to set up the scrollview
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(320, 800)];
which worked. But using autolayout prevents scrolling. Where am I going wrong?
I found the right way to do it. Put a scroll view in the view controller, set x and y 0, make height bigger than screens view, e.g 1500. Place a view in the scrollview, set its x and y to 0, set its height a little less than that of the scrollview, eg 1200. Then pin this view to the scrollview. The button for pinning is and is found in the storyboard. This worked for me like a charm. The scrollview does not need to have its coordinates set in the .m file.
This worked.

UIScrollView won't work

I've been struggling with this all morning for more than 3 hours now and I'm literally going insane!
I'm creating a Storyboard app for iOS 6 using Xcode 4.6. I dragged and dropped a UIScrollView to a ViewController. After designing the viewable part of the Scroll view, using its handles I stretched it vertically and pushed it up a bit so that that part is visible for me to design the rest of the screen. After I was done I positioned the scroll view back to fit the view. But I did not resize the scroll view to match the size of the view from the IB.
I added the following line of code in the viewDidLayoutSubviews method (Used that method instead of viewDidLoad because of Chris' comment here).
self.scrollView.contentSize = self.view.frame.size;
The code gets executed but the scroll bars won't appear and it scrolling does not work at all! I went through almost all the questions and answers here on SO regarding this and tried everything but to no avail.
I attached the runnable Xcode project here as well.
Please tell me what I should do to get this working. I'm eternally frustrated with Apple.
Thank you.
It is quite easy though. Remove viewDidLayoutSubviews method and replace viewDidLoad with following code. Height 672 is hardcoded for now.. but it will change as per the content in the scrollview.
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 627);
self.scrollView.frame = self.view.frame;
}
And in the storyboard, perform following steps
1. Select View and go to size inspector.
2. Select Bottom Space from the contraints.
3. Edit Bottom space constraint and set constant value to 0 (it was -211).
Link is updated source code : http://www54.zippyshare.com/v/37137086/file.html
Hope this helps.
UIScrollViews will only scroll if their contentSize is greater than their frame. If it is less than or equal to their frame, they will just act like views.
If you want to test it out, try:
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width,self.scrollView.frame.size.height+200);

Resources