Why does this UIScrollView experiment fail? - ios

In my view contoller's viewDidLoad function, I have this guy:
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:self.view.frame];
scroller.contentSize = self.view.frame.size; // Tried w/ and w/o this
scroller.showsVerticalScrollIndicator = YES; // Tried w/ and w/o this
for (int x = 0; x < 10; x++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, x * 100, 100, 100)];
label.text = [NSString stringWithFormat:#"%i label", x];
[scroller addSubview:label];
}
[self.view addSubview:scroller];
It shows the first 8 labels OK, but the scroll view won't... scroll. It is just cut off. Any idea why?

It won't scroll because your contentSize is only set to the same size as the scrollview itself. The contentSize is the size of the scrollable area that the scrollview acts as a window in to. You need to set it wide enough to actually see your labels.
Based on your current code, your final label is in rect {0, 900, 100, 100}, so your contentSize needs to be at least CGSizeMake(100, 1000).

Related

How can I get the text to center in my scrollable UILabel?

The scrollable label works fine, but for some reason I cant get the text in the UILabel to center in the UIScrollView, it always stays left aligned. Here is an image of what I am talking about, and my code. (P.S. the UILabel is a subview of the UIScrollView, and the UIScrollView is a subview of the UITableViewCell.
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(cell.frame.size.width/21.5, 25, cell.frame.size.width - 40 , 20)];
scrollView.showsHorizontalScrollIndicator = NO;
[cell addSubview:scrollView];
scrollLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, 20)];
scrollLabel.text = secondaryLabel.text;
scrollLabel.font = secondaryLabel.font;
scrollLabel.backgroundColor = [UIColor clearColor];
scrollLabel.textColor = [UIColor whiteColor];
scrollLabel.numberOfLines = 1;
scrollLabel.textAlignment = NSTextAlignmentCenter;
[scrollLabel sizeToFit];
[scrollView addSubview:scrollLabel];
scrollView.contentSize = CGSizeMake(scrollLabel.frame.size.width, 20);
I think the code you written for UILabel is pretty much right and the center TextAlignment is there but the problem is you need to pass the right X & Y for UILabel Frame's. For now they are getting set to 0-0 and are adding on the Scrollview's starting frame. This might can solve your problem.

iOS 8 Custom keyboard UIScrollview isn't working

I have tried to add UISchollView in custom keyboard view but it shows view but not scrolling.
here is my code. i have also tried self.view but its not working either.
UIScrollView *scrollview1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
NSInteger viewcount= 8;
for (int i = 0; i <viewcount; i++)
{
CGFloat y = i * 50;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,50, 50)];
view.backgroundColor = (i%2==0)?[UIColor greenColor]:[UIColor blueColor];
[scrollview1 addSubview:view];
}
scrollview1.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *viewcount);
scrollview1.backgroundColor = [UIColor redColor];
scrollview1.pagingEnabled=YES;
[self.inputView addSubview:scrollview1];
UIScrollView will scroll if its contentSize is greater then its frame size. So try setting its contentSize property and also check if userInteraction is enabled or not.
Hope it will work.

How to rearrange a view according to the size of a label?

I have a label in a view setted with an autosize with the next code:
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
[labelsizeToFit];
My problem is the next:
When the label is smaller or bigger, the rest of labels are separated from this or overlaped each other.
How can I do to rearrange views (buttons, labels, etc.) automatically?
Thank you from a begginer iOS developer.
Get frame of first label and then manage other labels or views accordingly. Like this:
UILabel *firstLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 40, 30)];
[firstLbl setText:#"Any Text"];
[firstLbl sizeToFit];
CGRect frame = firstLbl.frame;
frame.origin.y = frame.size.height + frame.origin.y + 10;
UILabel *nextLbl = [[UILabel alloc] initWithFrame:frame];
Try this.

Adding a label to a scroll view in iOS

I would like to add a label to a scroll view. This is being done in main controller. However, when I do the following the label is on overlaying the contents of scroll view. I want the label to be exactly on top and then the rest of the scroll view contents.
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 80)];
label.backgroundColor = [UIColor greenColor];
label.text = #"Testing";
[self.scrollView addSubview: label];
[self.scrollView setNeedsDisplay];
Unfortunately, there is a little more to doing this programmatically. I'd highly recommend that you add this label to your xib or storyboard and not do this programmatically but if that isn't an option for what ever reason then there is only one thing you can do. You'll need to iterate over your scrollView's children and push each of the down slightly to make room for your new label, then set the label at the top of the scrollView.
Very simple example, this may not be working perfectly so you'll need to tweak it to what you need/want.
// Set the amount of y padding to push each subview down to make room at the top for the new label
CGFloat yPadding = 80.0f;
CGFloat contentWidth = CGRectGetWidth(self.contentView.frame);
UILabel* label = [[UILabel alloc] initWithFrame:(CGRect){CGPointZero, contentWidth, 44.0f}];
label.text = #"Testing";
// Iterate over each subview and push it down by the amount set in yPadding. Also, check for the subview with the lowest y value and set that as your labels y so that it is at the top of the scrollView.
for (UIView* subview in self.scrollView.subviews) {
CGRect subviewFrame = subview.frame;
CGFloat currentLabelY = CGRectGetMinY(label.frame);
// Set the labels y based off the subview with the lowest y value
if (currentLabelY == 0.0f || (currentLabelY > CGRectGetMinY(subviewFrame))) {
CGRect labelFrame = label.frame;
labelFrame.origin.y = subviewFrame.origin.y;
label.frame = labelFrame;
}
// Push the subview down by the amount set in yPadding
subviewFrame.origin.y += yPadding;
subview.frame = subviewFrame;
}
// Finally, add the label as a subView of the scrollView
[self.scrollView addSubview:label];
You should add the label as a subview of the scrollview:
[self.scrollView addSubview:label]

iOS scroll view is not working

I have a button at the 1000 height pixel mark and I need a uiscrollview in order to access that button. I implemented the scrollview code below and it shows the scroll view ,but doesn't allow me to scroll down. I might be missing a key function. Any tips or advice is appreciated.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 680)];
scrollView.contentSize = CGSizeMake(320, 1000);
scrollView.clipsToBounds = YES;
super.view.backgroundColor = [UIColor whiteColor];
[scrollView setUserInteractionEnabled:YES];
[[self view] addSubview:scrollView];
The problem is that your button's y coordinate is 1000 pixels, as you have stated. And the y-coordinate of scrollview's contentZize is maximum upto 1000 pixels as well. So the scrollview can jus scroll up to exactly where the button starts.
UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(50, 1000, 100,45);
[scrollView addSubview:btn];
set the content size of scrollview like this
scrollView.contentSize = CGSizeMake(320, 1100);
This will surely solve it.

Resources