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];
Related
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.
How would I get the actual dimensions of a view or subview that I'm controlling? For example:
UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0,0,200,100)];
[self addSubview:firstView];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 20, 230, 120)];
[firstView addSubview:button];
As you can see, my button object exceeds the dimensions of the view it is being added to.
With this is mind (assuming there are many objects being added as subviews), how can I determine the actual width and height of firstView?
If you would like to have the button have the same width and height as firstView you should use the bounds property of UIView like this:
UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0,0,200,100)];
[self addSubview:firstView];
UIButton *button = [[UIButton alloc] initWithFrame:firstView.bounds];
[firstView addSubview:button];
To just get the width/height of firstView you could do something like this:
CGSize size = firstView.bounds.size;
NSInteger width = size.width;
NSInteger height = size.height;
The first view really is 200 x 100. However, the views inside are not clipped unless you use clipsToBounds.
A preferable solution to your problem is to stop adding views that exceed the dimensions or to stop resizing views so that they overflow the available space, as appropriate.
(To actually calculate the full bounds including all subviews in the hierarchy is not hard, just a lot of work that won't actually get you anywhere. UIView happens to allow views to be larger than their containers, but it doesn't necessarily give any guarantees about what will work correctly and is likely to be a source of bugs if taken advantage of. The reason for clipsToBounds is probably that clipping is expensive but necessary during animation where subviews may be temporarily moved out of bounds for the purposes of an effect before they are removed from the view.)
firstview.frame.size.width
firstview.frame.size.height
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.
I was wondering what was the real meaning of using initWithFrame with this scrollView, because we also set the dimensions of the scrollView after that, and we add the scrollView as a subView of the view.
So why do we need to specify this initWithFrame? I actually don't really understand it when the frame is self.view.frame (I would understand it better if we set a different rectangle, such as 0,0 50,50)
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(847, 1129);
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
Thanks
self.view in this case is the view containing the scrollview, so the scrollview fills the entire view when set to self.view.frame. Frame and content size are different things - frame of scrollview defines visible part of scrollview, and content size defines the size of scrollable (zoomable) content inside your 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