UIScrollView within a panel not scrolling - ios

I'm trying to create a "GridView" style table within a UIScrollview which itself is within a restricted space. I put it within a Scrollview so that the user would be able to scroll down if there are more rows than can be displayed.
My storyboard for the relevant section looks like this:
To test it I tried filling it with UILabels, with this code:
for (int i = 0; i < 15; i++)
{
UILabel *cliLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, (60+(i*30)), 80, 40)];
UILabel *cliColum2Label = [[UILabel alloc] initWithFrame:CGRectMake(110, (60+(i*30)), 150, 40)];
UILabel *cliColum3Label = [[UILabel alloc] initWithFrame:CGRectMake(270, (60+(i*30)), 80, 40)];
UILabel *cliColum4Label = [[UILabel alloc] initWithFrame:CGRectMake(360, (60+(i*30)), 80, 40)];
[cliLabel setText:#"Column 1"];
[cliColum2Label setText:[NSString stringWithFormat:#"Column 2, row %d", i]];
[cliColum3Label setText:#"Column 3"];
[cliColum4Label setText:#"Column 4"];
[self.cliTableScrollView addSubview:cliLabel];
[self.cliTableScrollView addSubview:cliColum2Label];
[self.cliTableScrollView addSubview:cliColum3Label];
[self.cliTableScrollView addSubview:cliColum4Label];
}
This results in the following:
As you can see, there are more rows than can be displayed within the scroll view.
What I'd like to know is how to modify the above so that a scroll-bar appears, giving access to the out-of-bounds rows.

UIScrollView doesn't know how much it has to scroll, you need to tell it by providing contentSize. For example, at the end of your loop:
self.cliTableScrollView.contentSize = CGsizeMake(self.cliTableScrollView.frame.size.width, 60 + i * 30);

Related

iOS - addSubview without overlap

In a for loop, I'm trying to add Labels to a ScrollView based on a number given by the user. Here is the code in Objective-C:
NSInteger numberOfViews = [self.textfieldNumViews.text intValue];
for (int i = 0; i < numberOfViews ; i++){
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 300, 12)];
label.text = [NSString stringWithFormat:#"%d",i];
label.backgroundColor = [UIColor lightGrayColor];
[self.scrollviewViewContainer addSubview:label];
}
The problem is that the labels are overlapping each other inside the ScrollView, when I really want them to just display one after the other.
Image I understand that this is due to how addSubview behaves, placing views on top of other views, but is there any way I can prevent that? Or some other function I could use? I feel like this should be an easy task but I can't seem to figure it out. Is there something like a vertical layout property I can add to the ScrollView? Or add margins to the labels?
You need to set the y lower for each subview. Check the third line:
NSInteger numberOfViews = [self.textfieldNumViews.text intValue];
for (int i = 0; i < numberOfViews ; i++){
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, (i*20), 300, 12];
label.text = [NSString stringWithFormat:#"%d",i];
label.backgroundColor = [UIColor lightGrayColor];
[self.scrollviewViewContainer addSubview:label];
}

Create controls in UIView at runtime?

I have one UITextField and a button in a view.
Once a button is pressed, value in text field need to be searched fieldID column in core data table and get value[comma separated].
Separate the string by comma and assign values to array.
A new UIView needs to be inserted in the main screen and place N number[count of array] of labels and textfields and align.
I am able to do 1, 2 and UIView creation.
Can anybody help me to create N number of controls in sub UIView at runtime?
for(int i=0;i<self.yourArray.count; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,80*i+60,self.view.frame.size.width-20,60)];
label.text = [yourArray objectAtIndex:i];
[self.view addSubview:label];
}
Please Explain your problem more clearly !
May be your array count is very large so take a scroll view also in your view
UIScrollView* yourScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(yourView.frame.origin.x, yourView.frame.origin.y,yourView.frame.size.width,yourView.frame.size.height)];
for(int i= 0; i< 10;i++)
{
UILabel* lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, y, 50, 21)];
lbl.text = #"";
UITextField* txtField = [[UITextField alloc]initWithFrame:CGRectMake(lbl.frame.origin.x+10 , y, 200, 21)];
txtField.text = #"";
[yourScrollView addSubview:lbl];
[yourScrollView addSubview:txtField];
y = y+30;
}
[yourView addSubview:yourScrollView];

UIScrollView in UITableViewCell not scrolling

I am adding a cell to a UITableView which has a scrollview, the code I have in the cell is :
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 320, 44)];
UIView *labelView = [[UIView alloc] initWithFrame:CGRectMake(4, 4, 80 * _labels.count, 44)];
for (NSUInteger i = 0; i < _labels.count; i++) {
NSString *aLabel = [_labels objectAtIndex:i];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i * 70, 3, 60, 30)];
button.tag = i;
[button setTitle:aLabel forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor lightGrayColor]];
[labelView addSubview:button];
}
scrollView.contentSize = CGSizeMake(labelView.frame.size.width, labelView.frame.size.height);
[scrollView addSubview:labelView];
[self addSubview:scrollView];
However this does not scroll :(. If I have the same code segment outside the tableview and add this directly to the view, it works as expected. What am I missing?
Try [cell addSubview:scrollView];
Try adding the scrollView to the contentView of the UITableViewCell and maybe setting
self.contentView.userInteractionEnabled = NO
From the UITableViewCell reference doc
Discussion
The content view of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.

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.

Set name to UILabel as label1, label2 and so on

I want to set name to UILabel as label1,label2,label3 and so on. For that i don't want to write code for label1,label2,... Now i am using this method.
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
.....
But it's not a good method. I only want to write a single line of code for all labels.
How can i implement this? Is it possible to use %d along with label.
Please help.
just set tag of label and get data by using tag
for (int i=0; i<4; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
label.tag = i;
[self.view addSubview:label];
}
You can use tag of Label to differentiate from other instance. there is no such logic you are looking and it is not suggestible to differentiate objects.
for (int i = 0; i < 4; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
label.tag = i;
[self.view addSubview:label];
}
if ([self.view viewWithTag:i]) {
[(UILabel *)[self.view viewWithTag:i] setText:#"Update Text"];
}

Resources