uitextview text displaying on ios 7 but not on ios 6 - ios

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.

Related

Add rows and columns in UITableView iOS

Is it possible to add rows and columns to an UITableview in iPhone as shown below?
Please help me, I'm new to iOS development.
I found the answer.
UIView *newView124=[[UIView alloc]initWithFrame:CGRectMake(224, 90, 2, 48)];
newView124.backgroundColor=[UIColor grayColor];
[cell addSubview:newView124];
//Initialize Label
NSLog(#"cell width: %f",cell.frame.size.width);
UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 90, 100, 50)];
[lbl1 setFont:[UIFont fontWithName:#"FontName" size:12.0]];
[lbl1 setTextColor:[UIColor blackColor]];
lbl1.text = #"1";
[cell addSubview:lbl1];
UIView *newView=[[UIView alloc]initWithFrame:CGRectMake(40, 90, 2, 48)];
newView.backgroundColor=[UIColor grayColor];
[cell addSubview:newView];
UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(42, 90, 130, 50)];
[lbl2 setFont:[UIFont fontWithName:#"FontName" size:12.0]];
[lbl2 setTextColor:[UIColor blackColor]];
lbl2.text = #"Hospitsal";
[cell addSubview:lbl2];
UIView *newView1=[[UIView alloc]initWithFrame:CGRectMake(172, 90, 2, 48)];
newView1.backgroundColor=[UIColor grayColor];
[cell addSubview:newView1];
UILabel *lbl21 = [[UILabel alloc]initWithFrame:CGRectMake(174, 90, 50, 50)];
[lbl21 setFont:[UIFont fontWithName:#"FontName" size:12.0]];
[lbl21 setTextColor:[UIColor blackColor]];
lbl21.text = #"Active";
[cell addSubview:lbl21];
UIView *newView12=[[UIView alloc]initWithFrame:CGRectMake(224, 90, 2, 48)];
newView12.backgroundColor=[UIColor grayColor];
[cell addSubview:newView12];
UIButton *modeButton = [[UIButton alloc] initWithFrame:CGRectMake(250, 100, 30, 25)];
UIImage *ddd=[UIImage imageNamed:#"pencil.png"];
[modeButton setImage:ddd forState:UIControlStateNormal];
///[btnEdit addTarget:self action:#selector(imSelected) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:modeButton];
// [cell.contentView bringSubviewToFront:btnEdit];
UIView *aView1=[[UIView alloc]initWithFrame:CGRectMake(0, 135,self.view.frame.size.width, 2)];
aView1.backgroundColor=[UIColor grayColor];
[cell addSubview:aView1];

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.

ObjC - Programmatically add label to UIScrollView

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];

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);

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);

Resources