0
votes

I have a ViewController in which there is a UIScrollView. On that i have a UIView and the UIView I have to add Label and Image. The image should be above my label .

The Order is as Controller>>ScrollView>>UIView >> UILAbel >> UIImageView

but i am not able to add UIimageView to the view .

This is my code

 scrollView =[[UIScrollView alloc]init];
[scrollView setFrame:CGRectMake(0, 0, 320, 480)];
 [scrollView setContentSize:CGSizeMake(320,2820)];
[scrollView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
[scrollView setScrollEnabled:YES];
[scrollView setShowsHorizontalScrollIndicator:YES];
[scrollView setShowsVerticalScrollIndicator:NO];
[self.view addSubview:scrollView];


    UIView *firstView = [[UIView alloc]initWithFrame:CGRectMake(5, 5, 310, 300)];
firstView.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:firstView];


UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(65, 3, 180, 50)];
label.text = @"ABC";
label.textAlignment =NSTextAlignmentCenter;
label.textColor = [UIColor colorWithRed:74.0/255.0 green:144.0/255.0 blue:226.0/255.0 alpha:1];
label.font=[UIFont fontWithName:@"Helvetica-Bold" size:18];
[firstView addSubview:label];

 imgView = [[UIImageView alloc]initWithFrame:CGRectMake(115, 0, 90, 90)];
imgView.image = [UIImage imageNamed:@"thumbs4.png"];
[self.view bringSubviewToFront:imgView];

[self.view bringSubviewToFront:imgView] This line does nothing

2
Try : [scrollView addSubview:imgView];Tushar J.
@TusharJ. - Ya it appears after adding to scrollview, But i want its frame to be fixed . As I dont want it to be scrolledios
The ideal way to do this, in my point of view is, add imgView to self.view and make scrollView transparent. But I think, you are giving background color to scrollView [scrollView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];. Try this : [self.view addSubview:imgView]; [self.view bringSubviewToFront:imgView];Tushar J.
To bring subview of super view to front, first of all it must be added to the superview.Tushar J.
@TusharJ. - Thnks buddyios

2 Answers

1
votes

You should add image view to self.view before manipulate it. But it is not necessary to bring it to front if it will be the last added subview.

imgView = [[UIImageView alloc]initWithFrame:CGRectMake(115, 0, 90, 90)];
imgView.image = [UIImage imageNamed:@"thumbs4.png"];
[self.view addSubview:imgView] // added as subview
[self.view bringSubviewToFront:imgView]; // not really needed. imgView is actually in front
1
votes
UIScrollView *scrollView =[[UIScrollView alloc]init];
[scrollView setFrame:CGRectMake(0, 0, 320, 480)];
[scrollView setContentSize:CGSizeMake(320,2820)];
[scrollView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
[scrollView setScrollEnabled:YES];
[scrollView setShowsHorizontalScrollIndicator:YES];
[scrollView setShowsVerticalScrollIndicator:NO];
[self.view addSubview:scrollView];

UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(115, 0, 90, 90)];
imgView.image = [UIImage imageNamed:@"dream.jpg"];
[self.view addSubview:imgView];

UIView *firstView = [[UIView alloc]initWithFrame:CGRectMake(5, 5, 310, 300)];
firstView.backgroundColor = [UIColor blueColor];
[scrollView addSubview:firstView];


UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(65, 3, 180, 50)];
label.text = @"ABC";
label.textAlignment =NSTextAlignmentCenter;
label.textColor = [UIColor colorWithRed:74.0/255.0 green:144.0/255.0 blue:226.0/255.0 alpha:1];
label.font=[UIFont fontWithName:@"Helvetica-Bold" size:18];
[firstView addSubview:label];

I got Output like

enter image description here