ObjC - Programmatically add label to UIScrollView - ios

How do you programmaticaly add labels (or other controls for that matter) to a UIScrollView?
I used this code, but I couldn't scroll. Which leads me to believe that it doesn't add the labels to the UIScrollView.
This is my code:
for (int i = 1; i <= 30; i++)
{
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, (i * 50), 200, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setText:#"Dynamic"];
[self.Scroller addSubview:myLabel];
}
There are no errors, and it does create the labels, but like I said before, without scrolling.
What are we doing wrong?
Thanks in advance.

[self.Scroller setContentSize:CGSizeMake(self.Scroller.frame.size.width, (30 * 50)];

for (int i = 1; i <= 30; i++)
{
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, (i * 50), 200, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setTextColor:[UIColor blackColor]];
[myLabel setText:#"Dynamic"];
[self.Scroller addSubview:myLabel];
}
CGSize newContentSize=scroll.contentSize;
newContentSize.height+=30*50;
[self.Scroller setContentSize:newContentSize];

You are not setting contentSize for scrollView,Add this line in your loop:-
CGSize newContentSize=scroll.contentSize;
newContentSize.height+=i*50;
[self.Scroller setContentSize:newContentSize];

Related

Generated subview is always behind image view

viewController.m
-(void) viewDidLoad
{
[super viewDidLoad];
[self addScoreRowViewWithIndexLeft:(++i) andName:[scoreDict objectForKey:#"name"] andScore:[[scoreDict objectForKey:#"points"] integerValue]];
}
-(void) addScoreRowViewWithIndexLeft:(NSInteger)index andName: (NSString *)name andScore: (NSInteger)score {
UIView *scoreRowView = [[UIView alloc] initWithFrame:CGRectMake(128, 112 + index * 80, 280, 20)];
UILabel *indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 20, 20)];
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 20, 180, 20)];
UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(330, 20, 50, 20)];
UIColor *color = [UIColor darkGrayColor];
[indexLabel setText:[#((int)index) stringValue]];
[indexLabel setFont:[UIFont fontWithName:#"DINAlternate-Bold" size:18]];
[indexLabel setTextColor:color];
[nameLabel setText:name];
[nameLabel setFont:[UIFont fontWithName:#"DINAlternate-Bold" size:18]];
[nameLabel setTextColor:color];
[scoreLabel setText:[#((int)score) stringValue]];
[scoreLabel setFont:[UIFont fontWithName:#"DINAlternate-Bold" size:18]];
[scoreLabel setTextColor:color];
[scoreRowView addSubview:indexLabel];
[scoreRowView addSubview:nameLabel];
[scoreRowView addSubview:scoreLabel];
[self.view addSubview:scoreRowView];
}
I added my ImageView in my Storyboard as a subview of my View.
When I delete the ImageView, everything is ok. I get displayed my other code generated subviews. But when I add the ImageView in my Storyboard, it's on top of my other subviews. If I give my ImageView an alpha of 0.3 I can see my subviews shining through.
What's wrong with my code? I also tried;
[self.view bringSubviewToFront:scoreRowView];
but it doesn't work.
What can I do?
Try to change zPosition of your ImageView and scoreRowView. You can do this this way:
Connect your ImageView with your ViewController
In bottom of ViewDidLoad add something like that:
yourImageView.layer.zPosition = 1;
scoreRowView.layer.zPosition = 2;
I'm suspecting that [self.view bringSubviewToFront:scoreRowView]; doesn't work because you invoke it in ViewDidLoad where view in not fully work. You can try to invoke it in e.g. ViewDidAppear and check if that will work.

How to add Dynamic elements in the reverse direction to a Uiscrollview in IOS?

I had a scrollview in my application with dynamic elements which i am adding from right to left like this
if(array.count>0)
{
[[cell.contentView viewWithTag:777] removeFromSuperview];
float x=315.0;
float width=0.0;
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(70, 52,245,20)];
scroll.backgroundColor=[UIColor clearColor];
scroll.tag=777;
[scroll setContentOffset:CGPointMake(70,245)];
scroll.showsHorizontalScrollIndicator=NO;
for(int i =0; i <[array count] ; i++)
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundColor:[UIColor colorWithRed:133.0/255.0 green:155.0/255.0 blue:179.0/255.0 alpha:1.0]];
btn.layer.cornerRadius = 2;
[btn setTitle:[[array objectAtIndex:i] objectForKey:#“ggg”] forState:UIControlStateNormal];
btn.userInteractionEnabled=NO;
[btn.titleLabel setFont:[UIFont fontWithName:#"Helvetica Neue" size:10.0]];
CGSize size = [btn.titleLabel.text sizeWithAttributes:
#{NSFontAttributeName:
[UIFont fontWithName:#"Helvetica Neue" size:10.0]}];
x=x-(size.width+10);
//or whatever font you're using
[btn setFrame:CGRectMake(x,0,size.width+10,15)];
[scroll addSubview:btn];
width=width+x;
x=x-5;
}
scroll.contentSize=CGSizeMake((width+50),20);
// [scroll setPagingEnabled:YES];
[cell.contentView addSubview:scroll];
}
here everything working very fine,but i am not able to scroll to the last element added at the left.scroll was only allowing me to scroll up to 70 from left to right .Can anybody tell me where i am going wrong on this?
-(void)addButtonInScrollViewFromArray:(NSArray *)array{
NSInteger arrayCount=array.count;
NSInteger numberIteration=0;
CGFloat buttonXValue=300;
UIScrollView *buttonScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 360, 60)];
while (numberIteration<arrayCount) {
UIButton *buttonCreated=[UIButton buttonWithType:UIButtonTypeCustom];
buttonCreated.frame=CGRectMake(buttonXValue,5, 50, 50);
buttonXValue-=60;
[buttonCreated setTitle:[NSString stringWithFormat:#"Button%d",numberIteration] forState:UIControlStateNormal];
[buttonScrollView addSubview:buttonCreated];
numberIteration++;
}
CGFloat totalLength=numberIteration*60;
buttonScrollView.contentSize=CGSizeMake(totalLength,60);
}
this function you can pass the array
Can you try to below one
scroll.contentSize = CGSizeMake(x+size.width+10,20);

uitextview text displaying on ios 7 but not on ios 6

i have added multiple uitextfields programatically on uiscrollview its working fine and displaying text in ios 7 . but i am facing problem on ios 6 it does not display text its just display a large empty area with black background on uiscrollview this is the code
// set array values to label
-(void)addTextViews:(NSArray *)qualArray
{
//adding qualification label
UILabel *qualLbl;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
qualLbl = [[UILabel alloc]initWithFrame:CGRectMake(10, imgVw.bounds.size.height+20, 100, 20)];
[qualLbl setFont:[UIFont boldSystemFontOfSize:14.0]];
}
else
qualLbl = [[UILabel alloc]initWithFrame:CGRectMake(10, imgVw.bounds.size.height+20, 150, 20)];
[qualLbl setFont:[UIFont boldSystemFontOfSize:20.0]];
}
[qualLbl setTextColor:[UIColor colorWithRed:226.0f/255.0f green:73.0f/255.0f blue:20.0f/255.0f alpha:1.0]];
[qualLbl setBackgroundColor:[UIColor clearColor]];
[qualLbl setText:#"Qualification"];
[self.srcVw addSubview:qualLbl];
// adding text views
NSLog(#"img view height %f",imgVw.bounds.size.height);
height = imgVw.bounds.size.height + 55;
for (int i = 0; i < qualArray.count ; i++) {
UITextView *txtVw ;
UILabel *symbolLabel;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
txtVw =[[UITextView alloc]initWithFrame:CGRectMake(35, height, self.srcVw.bounds.size.width-50, 0)];
[txtVw setFont:[UIFont systemFontOfSize:12.0]];
symbolLabel =[[UILabel alloc]initWithFrame:CGRectMake(10, height, 25, 25)];
[symbolLabel setFont:[UIFont systemFontOfSize:12.0]];
}
else
{
txtVw =[[UITextView alloc]initWithFrame:CGRectMake(35, height, self.srcVw.bounds.size.width-50, 0)];
[txtVw setFont:[UIFont systemFontOfSize:20.0]];
symbolLabel =[[UILabel alloc]initWithFrame:CGRectMake(10, height, 25, 25)];
[symbolLabel setFont:[UIFont systemFontOfSize:12.0]];
}
[txtVw setScrollEnabled:NO];
[txtVw setEditable:NO];
[txtVw setText:[NSString stringWithFormat:#"%#" ,[qualArray objectAtIndex:i]]];
[txtVw sizeToFit];
[txtVw setTextColor:[UIColor whiteColor]];
[txtVw setBackgroundColor:[UIColor clearColor]];
[self.srcVw addSubview:txtVw];
// [txtVw setContentMode:UIViewContentModeScaleAspectFit];
//setting symbol label properties
[symbolLabel setText:#">"];
[symbolLabel setBackgroundColor:[UIColor clearColor]];
[symbolLabel setTextColor:[UIColor colorWithRed:226.0f/255.0f green:73.0f/255.0f blue:20.0f/255.0f alpha:1.0]];
[self.srcVw addSubview:symbolLabel];
height = height + txtVw.frame.size.height;
}
}
its code working perfect in ios 7 but i don't know what is problen when i run it on ios 6
you have set height of uitextview to Zero(0)
txtVw =[[UITextView alloc]initWithFrame:CGRectMake(35, height, self.srcVw.bounds.size.width-50, 0)];
just change it to 30
txtVw =[[UITextView alloc]initWithFrame:CGRectMake(35, height, self.srcVw.bounds.size.width-50, 30)];
because in iOS 7 it takes default size but in iOS 6 it not taking default value.
The best practice is to change the deltas in the interface builder if not you are not using autolayout.

UIView won't recognize a subview

I need to create a UIView with a UILabel subview programmatically because it works better than IB for animations. I cannot get the UIView to recognize the UILabel though. Here is my code from viewDidLoad:
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(764, 94, 200, 100)];
[label setText:#"Hello"];
[label setTextColor:[UIColor redColor]];
[label setBackgroundColor:[UIColor clearColor]];
UIView *hintView = [[UIView alloc]initWithFrame:CGRectMake(764, 94, 240, 198)];
[hintView setBackgroundColor:[UIColor colorWithRed:(34/255.f) green:(59/255.f) blue:(27/255.f) alpha:(255/255.f)]];
[hintView addSubview:label];
[self.view addSubview:hintView];
NSLog(#"subview %#", hintView.subviews);
Thanks
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(764, 94, 200, 100)];
You are setting wide frame try this
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
Change yourLable.frame.origin.x between (0) to (320 - withOfYourLabel)
Above code is same for your custom UIView (set X)
And if you want to get subViews of your custom view then follow
for(UIView *subView in hintViewsubviews)
{
/// here you got all subview of hintView
if([subView isKindOfClass:[UILabel class]])
{
// here you can also check that subView is UILabel or not ?
}
}
You are taking same frame size for both UIView and Label.bcz of that you are not getting proper output.See the changes which i have done to you code and try this
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(hintView.frame.origin.x+15, hintView.frame.origin.y+50, hintView.frame.size.width-60, hintView.frame.size.height-100)];
[label setText:#"Hello"];
[label setTextColor:[UIColor redColor]];
[label setBackgroundColor:[UIColor clearColor]];
UIView *hintView = [[UIView alloc]initWithFrame:CGRectMake(764, 94, 240, 198)];
[hintView setBackgroundColor:[UIColor colorWithRed:(34/255.f) green:(59/255.f) blue:(27/255.f) alpha:(255/255.f)]];
[hintView addSubview:label];
[self.view addSubview:hintView];
NSLog(#"subview %#", hintView.subviews);

iOS create UILabels dynamically

Sometimes I want my view to contain 5 UILabels, sometimes 3 and sometimes n.
The number of UILabels depends on data that's fetched from a website.
You'll have to make them in code instead of interface builder
for (int i = 0; i < n; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(/* where you want it*/)];
label.text = #"text"; //etc...
[self.view addSubview:label];
[label release];
}
A generic answer for a generic question:
while (labelsToDisplay)
{
UILabel *label = [[UILabel alloc] initWithFrame:aFrame];
[label setText:#"someText"];
[aViewContainer addSubview:label];
[label release];
}
NSArray *dataArray;
float xCoordinate=10.0,yCoordinate=10.0,width=100,height=40;
float ver_space=20.0;
for (int i = 0; i <dataArray.count; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(xCoordinate,yCoordinate,width,height)];
label.text = [dataArray objectAtIndex:i];
[self.view addSubview:label];
yCoordinate=yCoordinate+height+ver_space;
}
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(125, 12,170,20)];
lbl.text=#"IOS";
lbl.textAlignment = NSTextAlignmentCenter;
lbl.textColor = [UIColor whiteColor];
lbl.font = [UIFont fontWithName:#"AlNile" size:10.0];
lbl.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
lbl.layer.borderColor=[UIColor blackColor].CGColor;
lbl.layer.borderWidth=1.0f;
lbl.layer.cornerRadius = 6.0f;
[self.view addSubview:lbl];
UILabel *lblTitle=[[UILabel alloc]init];
[lblTitle setFrame:CGRectMake(0, 0, 100, 100)];
[lblTitle setText:#"MAK"];
[lblTitle setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:lblTitle];
-Here UILable will be created dynamically.
-but property will be set differently.
Create a TextView to show the text on the labels and a NSArray to contain the data.
For more information:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html
http://developer.apple.com/library/ios/documentation/uikit/reference/UITextView_Class/Reference/UITextView.html

Resources