view inside scrollView it is not moving - iphone

I have a UIScrollView of size 320x412 and inside it an UIView of size 280x277.But I tried for hours to make that View move up and down when I drag it.If I replace the UIView with a TextView it is working...I can drag the TextView up and down inside the UIScrollView...but with the View it is not working.
This is how my xib file looks like.The black one is the View I want to make it move inside the UIScrolView.ANy idea why is this not working?
EDIT: I did this
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.contentSize = CGSizeMake(320,480);
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
scrollView.bounces = YES;
scrollView.bouncesZoom = YES;
scrollView.scrollEnabled = YES;
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 5.0;
scrollView.delegate = self;
//myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView addSubview:myView];
[myView release];
And still not working!

You need to set the content size of scrollview greater then scrollview frame height to make the scrollview to scroll vertically. You scrollview height is 412.
Set content size, scrollview.contentSize = CGSizeMake(320, 430);

ALWAYS resize scrollview.contentSize .....
and not the scrollview.frame :===////
good luck! :=)

Related

iOS7 View moves under status/nav bars when loadView called again

I have a View Controller that creates its view programatically through the loadView method. The purpose of the view is to display a data models contents. When the view loads it is aligned correctly under the status and navigation bar.
But at a later point I present another view modally to allow the user to select a different data model to populate the view. This causes the the loadView to get called again and re-create the required UI for the new data model. The problem is that the view's contents now appear under the status and navigation bars!
Please note that I am not using auto-layout due to having to support iOS4 :( Also if I include the extendedLayout fix - it does fix the problem but only after the modal's dismiss animation completes leaving a jump down effect. My code below, thanks for any help.
- (void)loadView {
CGRect frame = CGRectMake(0, 0, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = YES;
self.view.backgroundColor = [Constants categoriesScreenBackgroundColor];
CGRect scrollFrame = CGRectMake(0, 0, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight - ToolbarHeight);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:scrollView];
_toolbarViewController = [self createToolbarViewController];
[self.view addSubview:_toolbarViewController.view];
_toolbarViewController.productInfoWorkflowState = _productInfoWorkflowState;
UIView *containerView = [[UIView alloc] initWithFrame:scrollView.frame];
containerView.backgroundColor = [UIColor clearColor];
_headerViewController = [self createHeaderViewController];
[containerView addSubview:_headerViewController.view];
_menuViewController = [[ProductInfoMenuViewController alloc] initWithBatch:[self batchData]];
_menuViewController.delegate = self;
[containerView addSubview:_menuViewController.view];
CGRect categoriesFrame = _menuViewController.view.frame;
categoriesFrame.origin.y = _headerViewController.view.frame.size.height;
_menuViewController.view.frame = categoriesFrame;
CGRect viewFrame = containerView.frame;
viewFrame.size.height = _headerViewController.view.frame.size.height + _menuViewController.view.frame.size.height;
containerView.frame = viewFrame;
[scrollView addSubview:containerView];
scrollView.contentSize = containerView.frame.size;
_starViewController = [[StarViewController alloc] initForProduct:_productData With:[StarredItems new]];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_starViewController.view];
}
Screen layout after first load:
Screen layout after second load:
With Leo's suggested fix, the scrollView is correct BUT the toolbar at the bottom now appears incorrectly. Code I used (placed after the toolbar creation code above):
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0")) {
self.automaticallyAdjustsScrollViewInsets = NO;
scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(64.0f, 0.0f, 0.0f, 0.0f);
scrollView.contentInset = UIEdgeInsetsMake(64.0f, 0.0f, 0.0f, 0.0f);
}
Results:
Add following code its work for me for same issue, may be its help you
/* ios 7 Change */
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
self.edgesForExtendedLayout = UIRectEdgeNone;
self.automaticallyAdjustsScrollViewInsets = NO;
}
Does this only happen on iOS 7?
Give this a try:
self.navigationController.navigationBar.translucent = NO;
Try following:
Toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
try this
CGRect frame = CGRectMake(0, StatusBarHeight, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = YES;
self.view.backgroundColor = [Constants categoriesScreenBackgroundColor];
CGRect scrollFrame = CGRectMake(0, StatusBarHeight, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight - ToolbarHeight);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:scrollView];
I think the problem is due to incorrect automatic set of content insets of the scrollview.
Try the following. In your loadView, set self.automaticallyAdjustsScrollViewInsets to NO (only if NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1). Now, set the contentInset and scrollIndicatorInsets to the correct values depending on OS version:
scrollview.contentInset = scrollview.scrollIndicatorInsets = UIEdgeInsetMake(NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1 ? 0 : 64, 0, 0, 0);
My problem was solved unmarking the check "Adjust Scroll View Insets" in the View Controller (using Storyboards).

iOS - UIScrollView is not working (it doesn't scroll at all - the image stays fixed)

I would like to display an image (width: 320 pixels, height: 1250 pixels) in an image view.
When I compile the application I get no scrolling at all. The view stays fixed.
What I did:
Added an UIScrollView via Interface Builder to my view.
Added an UIImageView via Interface Builder to my view.
Verified that UIImageView is below UIScrollView in Interface Builder.
Set the size of the UIScrollView with ViewDidLoad.
How do I do this?
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView.contentSize = CGSizeMake(320, 1250);
}
Screenshots:
ImageView:
ScrollView:
I just have done the same task..
Try this one.....
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
int scrollWidth = 120;
scrollView.contentSize = CGSizeMake(scrollWidth,80);
int xOffset = 0;
imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];
for(int index=0; index < [imagesName count]; index++)
{
UIImageView *img = [[UIImageView alloc] init];
img.bounds = CGRectMake(10, 10, 50, 50);
img.frame = CGRectMake(5+xOffset, 0, 50, 50);
NSLog(#"image: %#",[imagesName objectAtIndex:index]);
img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
[images insertObject:img atIndex:index];
scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
[scrollView addSubview:[images objectAtIndex:index]];
xOffset += 70;
}
Also set this one....
imagesName = [[NSArray alloc]initWithObjects:#"image1.jpg",#"image2.jpg",#"image3.jpg",#"image4.jpg",#"image5.jpg",#"image6.png",#"image7.png",#"image9.png",nil];
images = [[NSMutableArray alloc]init];
So for me the problem was that setting the content size didn't work in viewDidLoad(). I tried everything and I didn't understand why it wouldn't want to work, and then I tried the same stuff in viewDidAppear() and it magically worked...
From you last screenshot and from your comments it looks like your scrollView is way to big.
The scrollview must be visible on screen completely. For example a full screen UIScrollView on iPhone would have a size of 320 x 460.
If the scrollview is the same size as its content you can't scroll.
The greenish rectangle shows the size of your scrollview, the pinkish the size of your content (your image):
Xcode 11+, Swift 5
You can find the complete solution here.
I came across this same issue on iOS6 and the solution was to programmatically adjust the ContentSize.
So I will just quote from Raja (above) to show this:
CGSize scrollViewContentSize = CGSizeMake(320, 400);
[self.scrollView setContentSize:scrollViewContentSize];
NOTE: I was not having this issue on iOS5.. seems like iOS6 decided to do alot of prank just like the rotation/orientation saga
Since Xcode 5 it does not work like before. Also scrolling to the end of a scrollable text field makes problems. There are also differences between doing it on iPhone or iPad. On iPhone it worked without delayed timer.
This worked for me:
- (void)viewDidLoad
{
[super viewDidLoad];
NSTimer *timerforScrollView;
timerforScrollView =[NSTimer scheduledTimerWithTimeInterval:0.1
target:self selector:#selector(forScrollView)userInfo:nil repeats:NO];
}
- (void) forScrollView {
[scrollviewPad setScrollEnabled:YES];
[scrollviewPad setContentSize:CGSizeMake(768, 1015)]; // must be greater then the size in Storyboard
}
I found I had a similar problem but none of the above code worked. The issue was due to autolayout. I found that if I turned off autolayout by going to the storyboard clicking on Utilities -> File Inspector and unchecked Use Autolayout the scrolling did work (I also set scroll.contentSize = ...).
Sometimes autoLayout checkbox is ticked in the xib. That also creates this issue in xcode 5. Which makes the UIScrollView scrolling off.
Don't forget to add the category protocol to the interface, like this
#interface MyViewController : <UIScrollViewDelegate>
If you don't, you will not be able to set the scroll view delegate to self
(i.e. [MyScrollView setDelegate:self];)
If you do this, it should work.
My code is:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[contentScrollView setDelegate:self];
[contentScrollView setScrollEnabled:YES];
contentScrollView.contentSize = CGSizeMake(310, 500);
contentScrollView.frame = CGRectMake(5, 188, 310, 193);
}
Did you assign the scroll's view delegate? Always remember these:
[self.scrollView setDelegate:self];
[self.scrollView setScrollEnabled:YES];
The image view has to be a subview (so inside AND below) of the scrollview. From your description it seems they are paralell
You forgot one line. Add this to your view load function:
[scrollView setScrollEnabled:YES];
You could try disabling AutoLayout. In XCode 5 I tested all the above answers and I could only scroll it by disabling autolayout and activating all autosizing masks under the Size Inspector. The following code was used too:
self.scrollView.contentSize = CGSizeMake(320, 900);
self.scrollView.delegate = self;
self.scrollView.scrollEnabled = YES;
self.scrollView.frame = self.view.frame;
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 400);
self.scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
[self.view addSubview:self.scrollView];
CGSize scrollViewContentSize = CGSizeMake(320, 400);
[self.scrollView setContentSize:scrollViewContentSize];
scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSUInteger nimages = 0;
CGFloat cx = 0;
for (; ; nimages++) {
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", (nimages + 1)];
UIImage *image = [UIImage imageNamed:imageName];
if (image == nil) {
break;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = image.size.height;
rect.size.width = image.size.width;
rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
imageView.frame = rect;
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
Assuming that scrollView is a subview of view and fills it entirely you can use the following in viewDidLoad:
[scrollView setContentSize: CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];
I had a UIScrollView that was not scrolling and this allowed it to scroll.
Just add code
scrollView.contentSize = CGSizeMake(WIDTH,HEIGHT);
to method -(void)viewDidLayoutSubviews.
For more information checkout Stanford CS193p Lecture No 8 to understand View Controller Life cycle.
I had the same issue and was looking for the answer in this thread. I tried all the stuff, but nothing works. Then I found this:
.
You just need to deselect "Use Auto Layout" in File Inspector of your ViewController. Ta-Da, it works immediately for me. Enjoy.

Set starting point of content size for UIScrollView

How do I set the starting point of a UIScrollView? I would like to add a UIImageView left of the UIScrollView but changing the contentSize only adds scrolling room to the right of the scrollview. How do I add an ImageView left of the scrollView's (0,0) point and make it part of the scrollview's content size?
Hopefully I've got what you're trying to do here. I think this just takes a few turns with the contentOffset to get right.
Starting off;
Add the scrollView at frame (0,0,320,480) - its a full screen scroller
set contentSize to (320*3, 480) - it now has a content with the width of 3 'pages'
Add your imageView as a subview to the scrollView at frame (320,0,320,480)
set contentOffset of the scrollView to (320, 0) - this will move the content of the scrollView left, in the negative x direction by 320
Now your imageView will be on screen, but it will have a 320px width both on the left and right on the scroller content.
(Note that in the code below, i've simply added a UIView and not an imageView)
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.delegate = self;
scroller.pagingEnabled = YES;
scroller.backgroundColor = [UIColor blueColor];
scroller.contentSize = CGSizeMake(960, 480);
UIView *imgView = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 480)];
[imgView setBackgroundColor:[UIColor redColor]];
[scroller addSubview:imgView];
[scroller setContentOffset:CGPointMake(320, 0)];
[self.view addSubview:scroller];
Does that help?
Actually the best solution to start in the middle of the scroller if you are in a iPhone you should
[scroller setContentOffset:CGPointMake(320, 0)];
[self.view addSubview:scroller];
And for iPad
[scroller setContentOffset:CGPointMake(1024, 0)];
[self.view addSubview:scroller];
Try setting the Content offset.
[scrollView setContentOffset:CGPointMake(320, 0.0)];
If you're simply looking to set where the scroller content should be positioned at the start, you could do so with the - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated method which will scroll the content to the point specified in the method.
Let me know if this is not what you wanted to know, and I'll get back to ya!
Cheers.
The key to setting the starting point, is to assign bounds with xy coordinates different than zero. Like this
lazy var contentView: UIView = {
let size = CGFloat(5000)
let view = UIView(frame: CGRectZero)
view.frame = CGRectMake(0, 0, size, size)
view.bounds = CGRectMake(-size/2, -size/2, size, size)
return view
}()
func centerContent() {
let frame = UIEdgeInsetsInsetRect(self.scrollView.bounds, self.scrollView.contentInset)
let navigationBarHeight = self.scrollView.contentInset.top
let x = (self.scrollView.contentSize.width/2) - (frame.size.width/2)
let y = (self.scrollView.contentSize.height/2) - (frame.size.height/2) - navigationBarHeight
self.scrollView.contentOffset = CGPointMake(x, y)
}

UIscrollview resize problem

i have a srollview with textfields and labels as its subview, for two textfields i want to show UIpickerview.
For example: when user touches textfield11 i want to show a picker that slides up from the bottom of the screen, at this point i want to change height of my scrollview too, but it is not working.
CGRect scrollframe = scrollView.frame;
NSLog(#"scrollframe.height=%f, pick height=%f",scrollframe.size.height, pick.frame.size.height);
scrollframe.size.height -= pick.frame.size.height;
[UIView beginAnimations:#"start" context:NULL];
[UIView setAnimationDuration:0.2];
[UIView setAnimationBeginsFromCurrentState:YES];
[scrollView setFrame:scrollframe];
NSLog(#"scroll height = %f",scrollframe.size.height);
NSLog(#"scrollview height = %f", scrollView.frame.size.height);
[pick setFrame:CGRectOffset([pick frame], 0, -220)];
[UIView commitAnimations];
This is console log..
2011-06-08 10:43:31.316 AESDirect[281:207] scrollframe.height=416.000000, pick height=216.000000
2011-06-08 10:43:31.316 AESDirect[281:207] scroll height = 200.000000
2011-06-08 10:43:31.317 AESDirect[281:207] scrollview height = 200.000000
This might help you:
[scrollframe setContentOffset:CGPointMake(scrollframe.contentOffset.x, scrollframe.contentOffset.y-100)];
You can call the above code via the same function from where you are calling the pickerView. So this way you don't have to change the height of the scrollview.
Your code should be like this then:
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[contentView setBackgroundColor:[UIColor clearColor]];
self.view = contentView;
UIScrollView *horizontalScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[horizontalScroll setBackgroundColor:[UIColor blackColor]];
[horizontalScroll setDelegate:self];
[contentView addSubview:horizontalScroll];
UIPickerView *tempPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 220, 320, 100)];
[tempPicker setHidden:YES];
[contentView addSubview:tempPicker];
Make all three of them as Global in .h file and then you will have better control on their behavior. Your picker view is moving along with other items of scroll view because you might have add picker view as subview on your scroll view.
You have to change your contentsize and content offset and pickerview in view not in scroll view because if you add it in scroll view you will not be able to select values from picker view.

UIScrollView "back to original size"

I have a UIScrollView which has a UIView embedded in it. It scrolls up from bottom on a buttonClick, the button is located in another view. When it scrolls up it partly covers the calling view i.e view on which the button is located. Now, I have another button that is supposed to push this scroll view down. This button doesn't seem to be working. I have posted the entire code. Please Help!!
-(IBAction)unscrollClicked:(id)sender
{
//[viewController.view removeFromSuperview];
[UIView beginAnimations:#"back to original size" context:nil];
scrollView.contentSize=viewController.view.bounds.size;
//scrollView.contentSize=viewController.view.bounds.size;
[UIView commitAnimations];
}
-(IBAction)scrollClicked:(id)sender
{
//viewController.view.backgroundColor=[UIColor orangeColor];
viewController.view.frame=CGRectMake(0, 410, 320, 460);
//[self.view addSubview:viewController.view];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,40, 320, 400)];//(0,0,320,160)
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//[MyView setFrame:CGRectMake (0, 0, 320, 170)];
viewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
scrollView.contentSize = viewController.view.bounds.size;
scrollView.bounces = YES;
scrollView.bouncesZoom = YES;
scrollView.scrollEnabled = YES;
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 5.0;
scrollView.delegate = self;
[scrollView addSubview: viewController.view];
[self.view addSubview:scrollView];
[self moveScrollView:scrollView];
}
-(void) moveScrollView:(UIScrollView *)scrollView
{
CGFloat scrollamount=400;
[scrollView setContentOffset:CGPointMake(0,scrollamount) animated:YES];
}
Try using the zoomToRect method.
I think you should modify your move method. As it is, it shows content at a
fixed origin ( height 400), but after one invokation it should not do anything, since contentOffset is already at 400. Try this:
-(void) moveScrollView:(UIScrollView *)scrollView upwards:(BOOL)up {
CGFloat scrollamount=400;
if ( up)
[scrollView setContentOffset:CGPointMake(0,scrollamount) animated:YES];
else
[scrollView setContentOffset:CGPointMake(0,0) animated:YES];
}
And invoke like this:
[self moveScrollView:scrollView upwards:YES]; // or NO for moving down