When I set UIScrollView clipsToBounds property to NO, it allowed me to display the content views horizontally outside its bound. That is what I wanted to achieve. But it also allowed to scroll pass top and bottom bounds which I do not want.
Can UIScrollView clipsToBounds be applied vertically only?
(btw: I want to effect just as in the picture so I do not want to expand the content view horizontally.)
One solution would be to embed scroll view inside another view, which would have clipToBounds = NO, while scroll view would have clipToBounds = YES and width equal to content beign displayed.
As I am not good with images, I try example by code:
wrapperView.frame = CGRectMake(0, 0, 300, 300);
wrapperView.clipToBounds = NO;
scrollView.frame = CGRectMake(0, 0, 400, 300);
scrollView.clipToBounds = YES
[wrapperView addSubview:scrollView];
contentView.frame= CGRectMake(0, 0, 400, 1234);
[scrollView addSubview:contentView];
Related
I want to set the frame of a subview manually.
So I just create a CGRect with CGRectMake and use the new CGRect for the frame of the subview. The problem is that the subview don't show up.
When I just use the view.bounds property of the parent view and assign this as frame to the subview then everything shows up.
I also ensured that the frame is in the displayed area.
frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:[[UIView alloc] initWithFrame:frame]];
Good way to set subview's frame is
subview.frame = CGRectMake(0, 0,
CGRectGetWidth(self.view.bounds),
CGRectGetHeight(self.view.bounds));
Refer to link
All the values less than CGRectGetWidth(self.view.bounds) & CGRectGetHeight(self.view.bounds) should work fine
The code in your question will create a view to which you have no reference. This means it has no background colour, contents or other means to see it, and you have no means to set it. You've added the view, but it is invisible.
Create and configure the view first, assigning it to a local variable, before adding it as a subview. As part of this configuration, give the view a background colour or some subviews.
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
Is there a way to manually set a UITableView's width?
I would like to set the table as a subview which takes up half of the width of the screen and the full height.
Sure you can set the table frame to whatever you want.
self.tableView.frame = CGRectMake(80, 0, 160, screenHeight);
To set a UIView to take up the entire screen, is this correct?
self.mapView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
Or should I be using the self.view.bounds...?
You can use either of frame and bounds for view as frame and bounds are same for it. But when you work with subviews then use bounds because frame and bounds need not be the same. For subview they are same only when your subview's size is same as its superview.
I faced a strange problem, the scrollview does not scroll down, only scroll up. I have scrollview in my app, please look at my coding
.....
self.scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake(0, 0, 320,427)];
[self.view addSubViews: self.scrollView];
UIView *blueView = [[UIView alloc] initWithFrame: CGRectMake(0, 47, 320, 320)];
blueView.backgroundColor = [UIColor blueColor];
[self.scrollView addSubViews: blueView];
self.scrollView.contentSize = CGSize(320, 640);
....
My problem is no matter what value I changed contentSize, my ScrollView only scroll up, not scroll down. I want user can move blueView to the top or bottom of iPhone screen from the original position.
do you have this problem?
The Problem
It looks like your issue is with how you're orienting blueView within scrollView. You're setting the frame of blueView to the CGRect (0, 47, 320, 320). When you set the frame like this, one of the things you're implicitly saying is:
The top edge of blueView is 47 points below the top edge of scrollView.
That's a perfectly valid thing to say, but it's what's causing the problem you describe. scrollView won't scroll down because it is designed to start, by default, with the rect (0, 0, 320, 480) in view. The contentSize property only indicates the size of the content within the UIScrollView, not its positioning. When you set it, you're basically telling scrollView:
Starting from your content origin, the content is 320 points wide and 640 points tall.
Thus, scrollView won't scroll up because, as far as it knows, there's no content above the coordinate (0, 0).
The Solution
There are three steps you'll need to take to get the functionality you want.
Set the contentSize to be just big enough to allow blueView to scroll all the way up and down.
Put blueView in the vertical center of scrollView.
Scroll the scrollView so that it is initially centered on blueView.
Set the contentSize to be just big enough to allow blueView to scroll all the way up and down.
We'll want to calculate the correct value of the contentSize property. It is of the type CGSize, so we need two parts: width and height. width is easy – since you don't seem to want horizontal scrolling, just make it the width of the screen, 320. Height is a little more tricky. If you want blueView to just touch the top and bottom of the screen when scrolled up or down, you need to do some math. The correct total height will be double the height of the screen, minus the height of blueView. So:
scrollView.contentSize = CGSizeMake(320, 480 * 2.0 - blueView.frame.size.height);
Put blueView in the vertical center of scrollView.
That's easy; just set the center property of blueView:
blueView.center = CGPointMake(160, scrollView.contentSize.height / 2.0);
Scroll the scrollView so that it is initially centered on blueView.
If you check the Apple UIScrollView documentation, you'll see an instance method - (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated. This is exactly what you need to scroll scrollView programmatically. The rect you want is the one centered on blueView, with the size of the iPhone screen. So:
CGRect targetRect = CGRectMake(0, scrollView.contentSize.height / 2.0 - 240,
320, 480);
[scrollView scrollRectToVisible:targetRect animated:NO];
Make sure you do this scrolling in viewWillAppear, so it's ready right when the user sees the view.
That should be it. Let me know if you have any questions!
The content size of the scrollView should be the size of the view it is holding. This is how the code should be, try something like this.
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(X, Y, W, H1)];
UIView * blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, W, H2)];
self.scrollView .contentSize = blueView.frame.size;
[self.scrollView addSubview:blueView];
[self.view addSubView: self.scrollView];
Thanks to Riley. Here, the H1 is the height of the UIScrollVIew and H2 is the height of the blueView and (H1 < H2).