Image in UIScrollView changes position after scrolling the table - ios

I have a UITableView Cell that contains a UIScroller that has a UIImage in it.
I am having an issue where by when I first load the view, everything looks OK, but when I scroll up and down it changes.
Here is a screen shot of before:
And here is one after I do a scroll:
My code that loads the UIImage in the UIScroller is:
UIImageView *imageview = [[UIImageView alloc] initWithImage:myImage];
imageview.frame = self.scrollview.bounds;
self.scrollview.delegate = self;
[self.scrollview addSubview:imageview];
Can someone give me some pointers? I tried using code from the following stack overflow posts but they have not been able to do the trick so far:
UIImage Is Not Fitting In UIScrollView At Start
UIScrollView with centered UIImageView, like Photos app
Any suggestions?
Thanks

Setting the contentSize of your scrollview to the dimensions of your imageview might help.

Related

How to use UIScrollView

I am trying to achieve something similar to the photo options bar in VSCO Cam. I tried setting it up with an image, just to get an idea on how to do it, but its not working.
I think one of the problems could be with the fact that the UIScrollView should be horizontal.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.scrollView setContentSize:CGSizeMake(646, 73)];
self.scrollView.scrollEnabled = YES;
UIImage *bleh = [UIImage imageNamed:#"Digits 2 1"];
UIImageView *raaaa = [[UIImageView alloc]initWithImage:bleh];
[self.scrollView addSubview:raaaa];
}
So the first thing you need to understand is the contentsize property.
The subviews inside the scrollview are its "content". They can be arranged in any way you want inside the view.
In order to make it scroll though, you need to let the scrollview know how much "space" its content is taking up.
In your case, I'm going to guess that your image is 646 x 73 in dimensions. If you set the contentsize to that, the scrollview doesn't need to scroll because it can show all the content at once. However when you increase it, the scrollview now knows there is more content outside of the screen and it will allow you to scroll.
The second thing you need to understand is the position of the subviews. If you set the frame of your imageview to (0,0,646,73), it will be in the "left-most" position inside the scrollview. Thus, it will only scroll to the right (the white space you see).
Try this code and see if you can understand whats happening:
[self.scrollView setContentSize:CGSizeMake(1292, 73)];
UIImage *im = [UIImage imageNamed:#"Digits 2 1"];
UIImageView *v1 = [[UIImageView alloc]initWithImage:im];
UIImageView *v2 = [[UIImageView alloc]initWithImage:im];
v1.frame = CGRectMake(0,0,646,73);
v2.frame = CGRectMake(646,0,646,73);
[self.scrollView addSubView:v1];
[self.scrollView addSubView:v2];

UIScrollView in UIView not scrolling through ENTIRE content

Currently I am having a very trivial issue with my UIScrollView that lies within a UIViewController's UIView.
At the moment, I am using a xib file because alot of the info that I am putting inside of the UIScrollView is way too much for me to code.... (ALOT OF INFO, IMAGES, blah blah blah) but their all static.
This is the code I have used to add the UIScrollView to the UIViewController's UIView:
_scrollView = [[UIScrollView alloc] init];
_scrollView.delegate = self;
_scrollView.scrollEnabled = YES;
_scrollView.showsVerticalScrollIndicator = YES;
_scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 7 );
[self.view addSubview:_scrollView];
As you can see, the UIScrollView is LARGE!!!
I set it's content size to 7 times the size of the UIView AND set the delegete method to conform to the UIViewController.
Now, I am able to scroll perfectly BUT I am unable to scroll through the entire UIScrollView's content, even though I set it's content size to A LARGE amount!!!
What the heck am I doing wrong? :(
I also commented out this:
//_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.bounds.size.width, 2389)];
But when I use (^) line of code in comparison to the below (/) line of code.... No scrolling can be made....
_scrollView = [[UIScrollView alloc] init];
What am I doing wrong?! /sob....
Thank you!
EDIT:
The UIScrollView is now HALF THE SIZE of the UIView in xib.....Everything was setup in xib as well... this is ALL the CODE there is to be seen...
If you want some snapshots... here....
The UIScrollview in the first picture is scrolled down ALL THE WAY.....
I also NSLOG(#"self.view.bounds.size.height * 7) -> I get 2000+
Well because I cant see all of your code or its results I can only assume that the UIScrollView is larger then the screen.
This means that some of the content is hidden outside the bounds of the screen.
Hope this helps :)
It's a bad idea to place all of the views directly in the scroll view.
try putting them into one view that only it lives directly within the scroll view.

UITableView Header View Width

I'm trying to customize the width of my headerview on a UITableVIew. Unfortunately no matter what parameters I give it in the setframe method it always seems to have a width that is same as the tableview. I couldn't find an answer that talked about this, most answers seemed to talk about the section headers as opposed to the talbeviewheaders. Can anyone tell me of a way to customize the width of a tableview header?
Edit:
(void)viewDidLoad {
UIImageView *myImage =[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"takephoto.png"]];
[myImage setFrame:CGRectMake(0, 0, 50, 100)];
[[self myTable]setTableHeaderView:myImage;
}
Add myImage as the child of another view. The containing view will be automatically resized but you can then configure myImage to be the size you require.

UIView subview not resizing

I am working on a Universal Application. I have a View-based application which has one view controller. I am adding a UIImageView as a subview.
My problem is that if I put the ImageView in viewDidLoad, it is getting resized in iPad.
But if I add the ImageView to view controller dynamically on button tap event, the UIImageView is not getting resized as per iPad. Instead, it is showing ImageView with 320 x 480 dimensions.
Note: My View Controller has setAutoresizesSubviews to YES.
My code is as below:
-(void)buttonTap
{
UIImage *img = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"main-bg" ofType:#"png"]];
UIImageView *imgViewMainBG = [[UIImageView alloc]initWithImage:img];
imgViewMainBG.frame = CGRectMake(0,0,320,480);
imgViewMainBG.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:imgViewMainBG];
[imgViewMainBG release];
[img release];
img=nil;
}
AutoresizingMask is for changing the size of all subviews according to the change in size of the superView whenever the superView's size is changed, it won't resize subviews at the time of adding a subView.
But you can find the bounds or frame of self.view and set the frame property of imageView according to that bounds like below
[imgViewMainBG setFrame:CGRectMake(0, 0, self.view.frame.size.width,
self.view.frame.size.height)];
so that imgViewMainBG is fully visible in ipad
Try changing image view's contentMode property to UIViewContentModeScaleToFill.

a problem to load imageView to scrollView

I am an new user of Objective-c. I have a problem to load imageview to scrollview.
I use interface builder to add a scrollview onto view. and then I try to add an imageview on the scrollview by code.
UIImage *image = [[[UIImage alloc]init]autorelease];
image = [UIIamge imageNamed:#"cover.jpg"];
imageView = [[[UIImageView alloc]init]autorelease];
imageVIew.frame = CGRectMake(50.0, 40.0, 200.0,200.0);
imageView.image = image;
[scorllView addSubview:imageView];
then I add another imageView onto the ScrollView with position at (50.0,1000.0)
and length = 200, width = 200 (the screen of ipad is 786*1004)
the photos can appear on the screen. The second photo is not complete, and I try to scroll the screen, however I can't scroll it.
Thanks.
first, you only need
UIImage * image = [UIImage imageNamed:#"cover.jpg"];
class methods(imageNamed: here) that creates the instance of it other than "alloc" would create a autoreleased object.
[UIImage imageNamed:#"cover.jpg"]
is conceptually identical to
[[UIImage alloc] initWithFileContents:#"cover.jpg"] autorelease] // note, alloc once, release once.
(it may not be identical internally, imageNamed: caches the image initWithFileContents: might not)
In order to scroll the scrollView,
there are many things you need to make sure.
One of it is to make sure your scrollView's contentSize is bigger than your scrollView's frame size.
try setting your scrollView.contentSize = CGSizeMake(someBigValue, someBigValue);
The content inside scrollView scrolls inside the scrollView. So your content should be bigger than scrollView's frame size to scroll.
you have to increase contentinset or UIScrollview to make it scrollable,
You can increase bottom,
scorllView.contentInset = UIEdgeInsetsMake(0, 0, 680, 0);
or increase as you need

Resources