Problem with UIScrollView Content Offset - iphone

Im developing an iPad app in which I have a scrollview, the size of which i determine dynamically depending on the number of objects i display in it. Now, though i have not set any content offset I find that the scroll view has been offset by some amount whenever the view loads. When I check the value of the offset, it shows 0. I dont understand why without having a contentOffset value, the scrollview behaves that way. Any reasons for this?
I suspect this has something to do with iOS 4.2 as i dont remember this problem occurring when i was testing the app on iOS 3.2.
Update: The amount its getting offset seems to vary based on the contentSize of the scrollView. If the contentSize of the scrollView matches that of its frame height (its a vertical scrollview), then there is no offset. As the contentSize increases above the value of the height of the scrollView frame, the offset also increases proportionally. However, scrollView.contentOffset.y still = 0.0

if u use interface builder -
add scrollview to the view
create another view and add elements that u need to add to scroll view
then add this second view to scrollview
write this code in Viewdidload
scrollview.contentsize.y = lastelement.frame.origin.y + lastelement.frame.size.height + 10
this will adjust the scrollview to content size. it displays the first element and can scroll from top to bottom.

The UIScrollView definitively does not behave the same way on iPad with iOS 4.2.1 than in iOS 3.
I don't yet know if it's a bug or if I missed something in the documentation about an evolution.
My scrollview was created in a xib and I was reusing it to display different object of the same class. In my code, I only modified the contentSize of this scrollview.
The offset remained at 0 when looking with NSLog, but was varying on screen.
To fix my problem, I ended up creating programmatically a new scrollview each time I was displaying a new object. Might not be possible in your case, but for me, problem solved worked around.

I faced same problem. Solved by keeping code for setting scrollview in viewWillAppear:animated
-(void) viewWillAppear:(BOOL)animated
{
scrollView.frame = self.view.frame;
scrollView.contentSize = self.view.frame.size;
}

If you use Auto Layout and want to set the offset for the scroll view contents, this is what worked for me:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.myScrollView.contentOffset = CGPointMake(500, 0);
}

Assign the sum of frame.origin value and frame.height value of last element to scrollView.contentOffset.y

Also in Swift
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.contentOffset = CGPointZero
}

Related

Storyboard UIScrollView contentSize?

I feel like I have touched on every single possible cause for stopping this, but I have a UIScrollView in my Storyboard hooked up with an outlet and in the viewDidLoad I set the contentSize so that I can scroll (yes bigger than my frame size)!
However, whatever I change, I just can't scroll! I have a couple of textfields in my scrollview and bouncing enabled so I can see that when testing its moves up and down with my subviews in it but whatever I set the contentSize to I just can't scroll.
Anything I might be missing/should check? Is this a known issue with UIScrollView being used in a storyboard?
Whats even stranger is, I can do something like this:
[scrollView setBackgroundColor:[UIColor blueColor]]; and I have a blue scroll view! But setting content size fails.
Edit
My only code (otherwise scrollview is just dropped into storyboard view controller):
-(void)viewDidAppear:(BOOL)animated
{
[scrollView setContentSize:CGSizeMake(320, 640)];
}
Logged frame, comes out as expected:
width: 320.00
height: 504.00
Edit 2
Turns out that removing any subviews of the scroll view in my storyboard lets it scroll just fine. If I add any subview to it at all via the storyboard, even a blank brand new UIButton it just won't apply the contentSize/allow scrolling.
use ViewDidLayoutSubview
- (void)viewDidLayoutSubviews
{
[_scrollView setContentSize:CGSizeMake(320, 500)];
}
UIViewController's method invoke sequence is as below
awakeFromNib
viewDidLoad
viewWillAppear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear
viewDidLoad is not a good place to put code that relies on frame sizes of IB objects. If you log the contentSize of your scroll view in viewDidLoad, you will see that it's (0,0). Move the code (where you set the content size) to viewDidAppear, and it will work properly.
Check these
User Interaction enabled
Outlet connected
Included contentsize greater than bounds
scrolling Enabled
eg
scrollView.contentSize = CGSizeMake(320, 640);
My storyboard looks like this for scrollview [working]
I had exactly the same line of code in viewDidAppear and it did not work
Moved it to viewDidLayoutSubviews and it worked correctly.
[scrollView setContentSize:CGSizeMake(320, 500)];
Thanks trick14 for the answer.
The issue is most probably with Auto Layout. UIScrollView needs special attention when using AutoLayout.
Quick-fix - bind one of the scroll's subviews to the top AND bottom space of it's superview (the scroll view).
Long story:
Questions on SO:
UIScrollView not scrolling regardless of large contentSize,
UIScrollView will not scroll, even after content size set,
UIScrollView doesn't use autolayout constraints
Apple's Documentation:
https://developer.apple.com/library/ios/technotes/tn2154/_index.html
Trip14's answer worked for me. In swift I coded it as:
override func viewDidLayoutSubviews() {
(self.view as! UIScrollView).contentSize = CGSizeMake(600, 600)
}
This seems to be a similar issue. Other Story
It might be an issue with auto layout or constraints in the storyboard.
the best way with the storyboard.:

iOS UIScrollView in UIWindow

I currently am making an iOS project in which I have a UIScrollView as a direct subview of a UIWindow (using [window addSubview:scrollView];). The window's frame and its content are being set properly, and the contentSize is set to be bigger than the window's frame. When I try to scroll the UIScrollView, it doesn't scroll at all. Both scrollEnabled and pagingEnabled are set to YES, but the scrollview doesn't scroll, which leads me to believe that the touch/scroll events are not even being received by the scroll view. The window has a UITapGestureRecognizer added to it if it makes any difference. Do I need to somehow forward the swipe events to the UIScrollView, or is there a different reason that it's not scrolling?
EDIT: Here's some code.
float count=ceil([self.msgArray count]/2); //msgArray has length of 3+, NSLog()'d and confirmed.
float contentHeight=97.5 * count;
[dataScrollView setContentSize:CGSizeMake(320,contentHeight)];
dataScrollView.userInteractionEnabled=YES;
dataScrollView.pagingEnabled=YES;
dataScrollView.scrollEnabled=YES;
dataScrollView.clipsToBounds=YES; //Have also tried with this set to NO, or not set at all.
//Add subviews to dataScrollView.
EDIT: Here's some more info.
contentHeight is 195.00 when logged. I've removed the delegate method and I am back to using direct subviews of the scroll view. The window's height is 97.50.
EDIT: I've also removed the UITapGestureRecognizer from the UIWindow, but the scrollview still doesn't scroll.
Ah, the UITapGestureRecognizer! This might be a bug that took me hours to figure out in my own project. Is its cancelsTouchesInView property set to NO like it should be? (YES is the default... It can really throw you off if you're not expecting it.)
Just log the scrollview bounds width/height. The content height you are setting should be greater than the scrollview height. If its more then the scroll view automatically enables its scrolling.

UIScrollView made in storyboard has frame value of zero

I made a scrollview in my storyboard which contains severals UIImageView.
The problem is, the frame of that scrollview is equal to {0, 0, 0, 0} and I don't know why. The scrollview is visible on my screen, but I'm not able to scroll it.
I already try to set a content size, and a frame, but without success.
Fixed it out !
In fact, I develop the app on iOS 6.0 and I had to uncheck the "use autolayout" checkbox on storyboard properties...
Thanks to all anyway !
I was getting this problem too but I need to use auto layout.
I was trying to set an UIImageView as a subview then do some calcs to work out the minimum, maximum zoom scales and set the zoomScale.
The problem was that I was trying to set zoomScale to zero as the scroll views frame was zero which was giving me this error:
<Error>: CGAffineTransformInvert: singular matrix.
So I setup the image and imageView in viewDidLoad / viewWillAppear (and hide the imageView) and then set the zoomScale in viewDidAppear (then unhide imageView).
This seems to work well, although I can't say for sure why. I guess it gives the scroll view a chance to set its bounds correctly.
The issue here is AutoLayout, as some of the other answers have indicated. Specifically, the issue is that autolayout does not occur until after viewWillAppear, and if you put your code in viewDidAppear, you will get funky display artifacts as you change things on the screen while the user is watching...
If you need AutoLayout enabled, the best place to put your frame dependent code is in:
- (void)viewDidLayoutSubviews
Keep in mind that this gets called again and again though, so if you only want some initialization code run once, when the view is displayed, then you can create a BOOL property that stores whether the initial layout has already been done.
#property (assign, nonatomic) BOOL initialLayoutComplete;
And then you can write your viewDidLayoutSubviews this way:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (!self.initialLayoutComplete) {
// frame dependent code here...
}
}
As I mentioned in this answer: The frame of the scrollview will not be initialised until you are in the viewWillAppear method.
This seems to be a new behaviour in iOS 6, not sure if it's a bug or an intentional change.
You should be able to use auto layout without worrying about this.
Did you connect IBOutlet?
Did you init scrollView with this code:
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 280, 360)];
self.scrollView.contentSize = CGSizeMake(500, 360);
//values are of course only example values
How you add UIImageView to scrollView?
I think one clarification is in place to Enrico comment below.
The frame size will have the right values at viewDidAppear (and in viewWillAppear the value will be set, but they are still zero in that method)

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!!

UIScrollView not showing scroll indicator

I have a UIScrollView which I create and size dynamically using...
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width , length);
I then add subviews to the UIScrollView.
I do have scrollView.showsVerticalScrollIndicator = YES;
When scrolling the scroll indicator never appears.
Even if I call [scrollView flashScrollIndicators] nothing happens.
Ideas?
Had the same problem and couldn't find the cause. The last answer gave the final hint: whenever I added new subviews I first removed all existing subviews from the scrollview (and apparently also the scroll indicators).
After checking in my loop, if the subview really was of the kind I wanted to be removed the scroll indicators showed up:
for (NSObject * subview in [[[scrollView subviews] copy] autorelease]) {
if ([subview isKindOfClass:[MySubView class]]) {
[(MySubView*)subview removeFromSuperview];
}
}
Update: changed code to Nikolai's suggestion
When I've dealt with this before, in my implementation of a grid, I would occasionally get some cells over the top of the scroll indicator. To fix this I am now inserting subviews at index 0 rather than adding them, which adds them to the top. So try something like this:
[scrollview insertSubview:subview atIndex:0];
For me, the horizontal indicator had mysteriously disappeared in my app on iOS 7. Later found out that for some strange reason, I had to enable both Shows Horizontal Indicator and Shows Vertical Indicator to make the horizontal one show up. If I set it to not show the vertical indicator, it would also not show horizontal indicator.
I fix this by adding this code after add new subview:
self.showsVerticalScrollIndicator = NO;
self.showsVerticalScrollIndicator = YES;
It will also happen (at least in the case of a UITableView) if the contentSize is too small for the table view to scroll. If you have enabled bouncing, then the tableview does not actually scroll and does not display the indicators therefore. Try fitting more content inside.
It can happen also if the parent of the scrollview is smaller horizontally than the scroll view itself :
The scroll bar is stuck to the right side of the ScrollView / TableView and this right side is not visible due to the parent bounds ( with a clipToBounds hidding it for instance).
I've seen this issue so I share it in case it can help.
Just check the width of your ScrollView's frame not to be bigger than the width of its parent view frame.
Two conditions,
If you are using a storyboard
If you are using a UITableView inside a UIViewController
Then, you should check your indicator insets are set to 0 (or any other number that is relevant to your autolayout):
Noticed this when the UIScrollView was a 48 px tall horizontal band, scrollable horizontally. Maybe Cocoa decides the area is too small for a scroll indicator...