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.
Related
I want to have a UIScrollView on a panoramic image so that you can pan across the image horizontally. For some reason, the code I'm using does not allow the user to scroll horizontally.
Why won't it scroll horizontally?
Here is the code:
[scrollView setFrame:CGRectMake(0,160,1338,269)];
scrollView.scrollEnabled = YES;
scrollView.userInteractionEnabled = YES;
UIImageView *panorama = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 1338, 269)];
panorama.image = [UIImage imageNamed:#"Panorama.png"];
scrollView.contentSize = CGSizeMake(320, 269);
[scrollView addSubview:panorama];
[self.view addSubview:scrollView];
The scrollView's frame is stationary, the contentSize is the one that allows the scrolling.
scrollView.contentSize = CGSizeMake(320, 269);
Should be replaced with
scrollView.contentSize = CGSizeMake(1338, 269);
To allow a large contentSize to be scrolled through. The frame should be smaller.
scrollView.frame = CGRectMake(0,0,320, 269);
Think of the frame as the area that you see on the screen at any given time. Think of the contentSize as the area that the scrollView is capable of showing (much larger).
You are doing it other way around. Scroll view's content size must be set to the size of the large image you are trying to scroll.
Change contentsize to size of the uiimageview and change scrollview frame to 320,269
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];
I have the following ViewController:
It contains two UILabels at top, an UIImageView, below it a UITextView and below this a UIButton. I have arranged them using the Interface Builder following the blue line. All of this controls are inside a UIScrollView:
[self.scrollView setContentSize:CGSizeMake(320, 660)];
[self.scrollView addSubview:self.descriptionText];
[self.scrollView addSubview:self.descriptionImage];
[self.scrollView addSubview:self.titleLabel];
[self.scrollView addSubview:self.feedNameLabel];
[self.scrollView addSubview:self.load];
So when enabling Autolayout option, I just selected the ViewControler and then "Reset to Suggested Constraints in Description View Controller". But when I run the app, the scroll still appears for the entire page, but the only control scrolling is the UIButton. When scrolling up it will scroll below the UITextView.
I have made the UITextView to resize depending on the text, so I want my UIButton to always have the same distance to the UITextView. For that I have also set Vertical Spacing to the UIButton, but like this I don't have any scroll to my page.
Using the Autolayout for the first time, can I get some suggestions on what am I doing wrong ?
edit:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,660)];
[contentView addSubview:self.descriptionText];
[contentView addSubview:self.descriptionImage];
[contentView addSubview:self.titleLabel];
[contentView addSubview:self.feedNameLabel];
[contentView addSubview:self.load];
contentView.translatesAutoresizingMaskIntoConstraints = YES;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 660)];
//[scrollView setContentSize:CGSizeMake(320, 660)];
[scrollView addSubview:contentView];
[self.view addSubview:scrollView];
// to resize UITextField
[self.descriptionText sizeToFit];
[self.descriptionText layoutIfNeeded];
self.descriptionText.scrollEnabled = NO;
}
Autolayout is a bit tricky when it comes to UIScrollView subviews. I would recommend:
1-Embed all your controls (descriptionText + descriptionImage + titleLabel + feedNameLabel + load) into a UIView first, say contentView:
UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,660)];
//add all controls as subviews to contenView
2-Add contentView as subview of self.scrollView.
3-Keep the translatesAutoresizingMaskIntoConstraints property of contentView to YES.
I recommend you read this technical note from Apple here.
If you are using AutoLayout you don't have to set the content size of your scroll view. In fact, I believe it has no effect at all. It is set automatically from the constraints you are setting up. The trick is that you have to have at least one constraint related to every side of the scroll view, otherwise the content size will be wrong and it won't scroll. So for example, if you would have a really large image in it you would need 4 constraints connecting the sides of the UIImageView to the sides of the UIScrollView. You can read about this more here.
I have a view controller with a button. When the button is pressed, I want to add a subview which contains a scrollview. Right now my code displays the subview and scrolls fine as long as I don't add the xib. When I add the xib to the scrollview, the xib appears in the subview but there is no longer any scrolling.
The main View Controller:
-(IBAction)startNewSearch:(id)sender {
UIScrollView *myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 600)];
UIView *myView = [[[NSBundle mainBundle] loadNibNamed:#"NewSearch" owner:self options:nil] objectAtIndex:0];
[myScrollView setScrollEnabled:YES];
[myScrollView setContentSize:CGSizeMake(320, 600)];
[myScrollView setShowsVerticalScrollIndicator:YES];
[myScrollView setShowsHorizontalScrollIndicator:YES];
[myScrollView setPagingEnabled:NO];
[myScrollView addSubview:myView];
[self.view addSubview:myScrollView];
}
The NewSearch class is a UIView. It's xib is 320 wide by 600 high.
I'm not sure if I understood your question exactly, but it may or may not be similar to something I did.
See if adding this will work:
-(void)viewWillAppear:(BOOL)animated {
[self.myScrollView setFrame:CGRectMake(0, 0, 320, 600)];
}
Sorry if this isn't what you're looking for... I too am having issues with scrollview stuff. Good luck!
This happens because you have same content size height as a frame height.
Try to use same frame size as your main view on your screen and content size of the scroll view should be bigger then height (in your case) or width (this depends on which scroll you need horizontally or vertically).
For example
CGRect scrollFrame = CGRectMake(0, 0, [self.view bounds].size.height, self.view bounds].size.width);
UIScrollView *myScrollView = [[UIScrollView alloc]initWithFrame: scrollFrame];
...
[myScrollView setContentSize:CGSizeMake(320, **800**)];
As you see there is height of content size bigger then frame height of the your scroll view. So it can scroll horizontally.
Also see the UIScrollView documentation.
I encountered a similar problem when I started using ios8. It turns out that touches were being trapped by something in the chain of view controllers. When I started using presentView Controller for the view controller containing the scrollview, then touches (and scrolling)started working again.
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.