I am new in iOS programming, and my scroll view is not scrolling horizontally.
I want to make it possible to scroll horizontally, vertically it works correctly, but horizontally it not working please help me. I used the following code.
scrollView.frame = CGRectMake(0, 0, 600, 460);
[scrollView setContentSize:CGSizeMake(320, 678)];
You can set the frame of scrollview is CGRectMake(0, 0, 320, 460) and content size is [scrollView setContentSize:CGSizeMake(600, 678)]; because scroll is enabled when the scroll content size is greater then the frame size of scroll view
Try the following . this is working code for me . I am adding the images on horizontal scroll view .
int scrollWidth=60;
for (int i=0;i<array.count;i++)
{
UIImageView *imageView1=[[UIImageView alloc]initWithFrame:CGRectMake(scrollWidth,8,50,40)];
imageView1.userInteractionEnabled=YES;
imageView1.backgroundColor=[array objectAtIndex:i];
[scrollView addSubview:imageView1];
scrollWidth=scrollWidth+80;
}
[scrollView setContentSize:CGSizeMake(scrollWidth, 30)];
You set the width 600 but the content size is 320 so in that case you can't scroll horizontally. So you need to set width of content.
[scrollView setContentSize:CGSizeMake(600, 678)];
//use
scrollView.frame = CGRectMake(0, 0, 320, 460);
scrollView.contentSize = CGSizeMake(600,678);
Related
I am displaying multiple labels inside a UIScrollView, I can get them to display and I can get them to scroll, but a label at the top of the screen is cut off and is not visible unless you pull down. I have a tried adjusting the UIScrollViews content size, frame etc. all to no avail. Please help! Code:
[self.view addSubview: scrollView];
scrollView.scrollEnabled = YES;
int scrollWidth = 120;
int scrollHeight = 1000;
scrollView.contentSize = CGSizeMake(scrollWidth,scrollHeight);
scrollView.frame = CGRectMake(0, 0, 320, 400);
Set the contentOffset to (0,0).
i have got a UILabel which is not static.
I want to resize my scroll view so that it fits the label.
Here is my idea now:
self.scrollView.contentSize = CGSizeMake(320.0, 92+self.contentLabel.frame.size.height);
92 stands for pixels from where my label start. (there is a heading too)
But it doesn't work, it seems to be connected with Interface Builder also.
Thanks guys.
You need to set the frame as well. The contentSize of the scrollview is just what is within, not the frame of the view itself. If the contentSize is greater than the frame it will result in scrolling.
CGSize buttonSize = CGSizeMake(320.0, 92+self.contentLabel.frame.size.height);
self.scrollView.contentSize = buttonSize;
self.scrollView.frame = CGSizeMake(0, 0, 320.0, buttonSize.width, buttonSize.height);
How do I set the starting point of a UIScrollView? I would like to add a UIImageView left of the UIScrollView but changing the contentSize only adds scrolling room to the right of the scrollview. How do I add an ImageView left of the scrollView's (0,0) point and make it part of the scrollview's content size?
Hopefully I've got what you're trying to do here. I think this just takes a few turns with the contentOffset to get right.
Starting off;
Add the scrollView at frame (0,0,320,480) - its a full screen scroller
set contentSize to (320*3, 480) - it now has a content with the width of 3 'pages'
Add your imageView as a subview to the scrollView at frame (320,0,320,480)
set contentOffset of the scrollView to (320, 0) - this will move the content of the scrollView left, in the negative x direction by 320
Now your imageView will be on screen, but it will have a 320px width both on the left and right on the scroller content.
(Note that in the code below, i've simply added a UIView and not an imageView)
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.delegate = self;
scroller.pagingEnabled = YES;
scroller.backgroundColor = [UIColor blueColor];
scroller.contentSize = CGSizeMake(960, 480);
UIView *imgView = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 480)];
[imgView setBackgroundColor:[UIColor redColor]];
[scroller addSubview:imgView];
[scroller setContentOffset:CGPointMake(320, 0)];
[self.view addSubview:scroller];
Does that help?
Actually the best solution to start in the middle of the scroller if you are in a iPhone you should
[scroller setContentOffset:CGPointMake(320, 0)];
[self.view addSubview:scroller];
And for iPad
[scroller setContentOffset:CGPointMake(1024, 0)];
[self.view addSubview:scroller];
Try setting the Content offset.
[scrollView setContentOffset:CGPointMake(320, 0.0)];
If you're simply looking to set where the scroller content should be positioned at the start, you could do so with the - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated method which will scroll the content to the point specified in the method.
Let me know if this is not what you wanted to know, and I'll get back to ya!
Cheers.
The key to setting the starting point, is to assign bounds with xy coordinates different than zero. Like this
lazy var contentView: UIView = {
let size = CGFloat(5000)
let view = UIView(frame: CGRectZero)
view.frame = CGRectMake(0, 0, size, size)
view.bounds = CGRectMake(-size/2, -size/2, size, size)
return view
}()
func centerContent() {
let frame = UIEdgeInsetsInsetRect(self.scrollView.bounds, self.scrollView.contentInset)
let navigationBarHeight = self.scrollView.contentInset.top
let x = (self.scrollView.contentSize.width/2) - (frame.size.width/2)
let y = (self.scrollView.contentSize.height/2) - (frame.size.height/2) - navigationBarHeight
self.scrollView.contentOffset = CGPointMake(x, y)
}
Within my view I create a scrollview with a width of 320 and a height of 70.
Responding to the user touching a button, I expand the scrollview, so it is 380(h) x 320(w) in size.
The code for this is shown below:
CGRect scrollviewFrame = CGRectMake(0, 30, 320, 380);
[scrollView setFrame:scrollviewFrame];
[self layoutScrollImages:YES];
CGSize srect = CGSizeMake([scrollView bounds].size.width, (kNumImages * kScrollObjHeight));
[scrollView setContentSize:srect];
As I debug this project, I can see that srect is 320 (w) x 8000 (h) in size; however my issue is the scrollable area (where the user can actually touch to scroll the scrollview) remains the same as when it was it's original size.
I'm obviously missing something, does anyone know what it is?
Edit: I have created a sample project to illustrate the issue I am having, it is available here: http://dl.dropbox.com/u/9930498/ScrollViewTest.zip
I think , you have issue with your below line of code.
CGSize srect = CGSizeMake([scrollView bounds].size.width, (kNumImages * kScrollObjHeight));
[scrollView setContentSize:srect];
debug your program and see the content of srect , Also let us know the value of below two constant
kNumImages,kScrollObjHeight .
You need to change the contentSize of the scrollview as well as the frame.
Maybe this will help aswell: UIScrollView Class Reference
Do this when setting the new size
scrollView.scrollEnabled = YES;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.showsVerticalScrollIndicator = YES;
[scrollView setFrame:CGRectMake(0, 30, 320, 380)];
CGSize srect = CGSizeMake([scrollView bounds].size.width, (kNumImages * kScrollObjHeight));
[scrollView setContentSize:srect];
[self layoutScrollImages:YES];
I've got a view which is shown in landscape mode and I want to add a label which is int he center of the view both horizontal and vertically.
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *lab = [[[UILabel alloc] initWithFrame:frame] autorelease];
lab.font = [UIFont boldSystemFontOfSize:16.0];
lab.textAlignment = UITextAlignmentCenter;
lab.text = #"No data!";
[self.view addSubview:lab];
However, its not the right width and its at the top of the view ?
The texts are vertically center aligned by default. If you make the frame height equal to super view (which covers whole window, I assume) height then it will be vertically center aligned in that view.
For horizontal alignment you need to set textAlignment property which you are already setting correctly. But you need to fix the frame width which should be 480.
CGRect frame = CGRectMake(0, 0, 480, 320);