I've just started working with XCode 5.1 and iOS 7.1.
Im having some problems with PartialViews containing a scrollview. I have this ). I marked in red the space the scrollview should occupy but its taking more space vertically and horizontally. The viewcontroller and view are defined in the storyboard as freeform of 500x500 and the scrollview is defined like:
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, rowHeight*[lstMatches count])];
scroll.contentSize = CGSizeMake(self.view.frame.size.width, rowHeight*[lstMatches count]);
//.....more elements here, added as subviews of scrollview
[self.view addSubview:scroll];
The problem is the next:
1) The scrollview is wider than its container so I can't click over right buttons. I tried changing the width of the viewcontroller and view to 800 (max width is about 750), but i cant click them.
Thanks for your help
try:
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height])];
scroll.contentSize = CGSizeMake(5000, rowHeight*[lstMatches count]);
[self.view addSubview:scroll];
PS: Change the contentSize.width of 5000 to something that suits your needs
Related
After 2 days behind the implementation of a UIScrollView, I give up to do it by myself, despite It should be simple but I am not able to make it.
I need a vertical scrollview with Autolayout (I have 2 backgrounds images that need to scale), I have Xcode 7.2, iOs deployment target 9.0 and I am using the storyboard
The scrollview is a "new account" form with a lot of content, So it can have a fixed height
I tried lots of options but no one of them work, at the momment I have a scrollview with a "content view" with all of the components.
Problems:
1.- How can I put more content in the scrollview? If I try to put more compoments(more than the height of ViewControler), it automatically moves my components to be inside the ViewController rectangle
2.- How I can make it scrollable? If I pin the content view to the top, bottom, left, right and make it with fixed height (like 1000) it doesn't scroll
Any help would be appreciated
Scrollview will scroll until the end of last UI object. But this is not the default behaviour of the scrollview. You should explicitly set the contentSize on your scrollview. Check below example.
-(void)addImage {
int count = 20;
UIScrollView * scView = [[UIScrollView alloc] init];
scView.frame = CGRectMake(30, 60, self.view.frame.size.width, 60);
UIButton * btn;
UIImageView * imgV;
float imgWidth = 44;
for(int i=0;i<count;i++) {
imgV = [[UIImageView alloc] init];
imgV.frame = CGRectMake(i*imgWidth, 0, imgWidth, 44);
imgV.image = [UIImage imageNamed:#"gitar"];
[scView addSubview:imgV];
}
[scView setContentSize:CGSizeMake(imgWidth*count, 50)];
[scView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:scView];
}
What the above method does is, it will add 20 images in scrollview and at the end after adding all the images to scrollview in for loop I set contentSize to Scrollview. This step makes my scrollview to scroll until the content available. My example showing for Horizontal scroll, you have to change this behaviour to vertical scroll.
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 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).
I have a simple viewcontroller with labels and text boxes. but to many to fit on one screen, so i would like to add a scrollbar. Do i need a scrollview for that? I only found tutorials about zooming with scrollviews. How to do it?
Yes, you should use UIScrollView. This is good example how to work with scrollView from Apple developer library
And documentation about UIScrollView
you need to add a scrollview and set its content size more than the actual size of scrollview. setting content size more than scrollview size will enable scrolling and will show scrollbars...
//Scroll Vertically
UIView *containerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 800)];
[containerview addSubview:your label];
[containerview addSubview:your textbox];
............
UIScrollView *scrv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scrv.contentSize = CGSizeMake(containerview.frame.size.width ,containerview.frame.size.height);
[scrv addSubview:containerview];
[self.view addSubview:scrv];
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.