Generated subview is always behind image view - ios

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.

Related

Unable to center a imageView in a tableView Footer

I am trying to center a label and an image in a tableView Footer
I can't seem to make it work for both device iPhone and iPad
Right now it is centered on the iPhone but that's because I hardcoded it.
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
[view setBackgroundColor:[UIColor clearColor]];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, self.view.bounds.size.width, 20)];
lbl.lineBreakMode = NSLineBreakByWordWrapping;
lbl.numberOfLines = 0;
[lbl setText:#"Powered By"];
[lbl setFont:[UIFont systemFontOfSize:10]];
[lbl setTextAlignment:NSTextAlignmentCenter];
[lbl setTextColor:[UIColor blackColor]];
District *school = [District new];
UIImageView * logoView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 25, 150, 37.5)];
logoView.image = [UIImage imageNamed:#"Logo.png"];
[logoView autoresizingMask];
[view addSubview:logoView];
[view addSubview:lbl];
return view;
}
I would like to center this view, without hardcoding it. I tried taking the screen size and divide by 2.
It did not center, what is the right approach, please advise.
You need to set the view's autoresizingMask as needed.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
[view setBackgroundColor:[UIColor clearColor]];
CGRect lblFrame = view.bounds;
lblFrame.origin.y = 15;
lblFrame.size.height = 20;
UILabel *lbl = [[UILabel alloc] initWithFrame:lblFrame];
lbl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
lbl.lineBreakMode = NSLineBreakByWordWrapping;
lbl.numberOfLines = 0;
[lbl setText:#"Powered By"];
[lbl setFont:[UIFont systemFontOfSize:10]];
[lbl setTextAlignment:NSTextAlignmentCenter];
[lbl setTextColor:[UIColor blackColor]];
District *school = [District new];
CGRect logoFrame = CGRectMake((view.bounds.size.width - 150) / 2.0, 25, 150, 37.5);
UIImageView * logoView = [[UIImageView alloc]initWithFrame:logoFrame];
logoView.image = [UIImage imageNamed:#"Logo.png"];
logoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[view addSubview:logoView];
[view addSubview:lbl];
return view;
}
This assumes the label should fill the width and the image should stay centered left-to-right.

How to scroll the header along with the UITableView?

I have a UITableView with a header. the problem I currently have is that the header doesn't scroll with the table. I need it to scroll off screen (above) when the user scrolls the table view up. the tableview scrolls but the header is locked at the top of the UIView.
thanks
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionHeader = [[UILabel alloc] initWithFrame:CGRectNull];
sectionHeader.backgroundColor = [UIColor whiteColor];
// add user profile image to _contentView
UIImageView *userImageView;
UIImage *userImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:userProfileImageUrl]]];
userImageView=[[UIImageView alloc]initWithImage:userImage];
userImageView.frame=CGRectMake(10,10,90,100);
[sectionHeader addSubview:userImageView];
// return userImageView;
// user name lable
CGRect userNameFrame = CGRectMake(110, 60, 100, 50 );
UILabel* userNameLabel = [[UILabel alloc] initWithFrame: userNameFrame];
[userNameLabel setText: firstName];
[userNameLabel setTextColor: [UIColor blackColor]];
[userNameLabel setBackgroundColor:[UIColor clearColor]];
[userNameLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:14]];
[sectionHeader addSubview:userNameLabel];
// user last name label
CGRect userLastNameFrame = CGRectMake(110, 75, 100, 50 );
UILabel* userLastNameLabel = [[UILabel alloc] initWithFrame: userLastNameFrame];
[userLastNameLabel setText: lastName];
[userLastNameLabel setTextColor: [UIColor blackColor]];
[userLastNameLabel setBackgroundColor:[UIColor clearColor]];
[userLastNameLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:14]];
[sectionHeader addSubview:userLastNameLabel];
// user checkin view
UIView *userCheckinView = [[UIView alloc] initWithFrame:CGRectMake(10, 120, 280, 25)];
userCheckinView.backgroundColor = customColorGrey;
[sectionHeader addSubview:userCheckinView];
// check in label
UILabel* userCheckInLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 2, 100, 20)];
[userCheckInLabel setText: #"CHECK-IN"];
userCheckInLabel.backgroundColor = customColorGrey;
userCheckInLabel.textColor = customColorIt;
[userCheckInLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:12]];
[userCheckinView addSubview:userCheckInLabel];
// image
UIImageView *checkinImg = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"classifica_geotag_C.png"]];
checkinImg.frame = CGRectMake(5, 0, 24, 24);
[userCheckinView addSubview:checkinImg];
// check in label
UILabel* userCheckInCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 2, 20, 20)];
[userCheckInCountLabel setText: [checkinCount stringValue]];
userCheckInCountLabel.backgroundColor = customColorGrey;
userCheckInCountLabel.textColor = customColorIt;
[userCheckInCountLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:12]];
[userCheckinView addSubview:userCheckInCountLabel];
// user like view
UIView *userLikeView = [[UIView alloc] initWithFrame:CGRectMake(10, 150, 280, 25)];
userLikeView.backgroundColor = customColorGrey;
[sectionHeader addSubview:userLikeView];
// like label
UILabel* userLikeLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 2, 100, 20)];
[userLikeLabel setText: #"LIKE"];
userLikeLabel.backgroundColor = customColorGrey;
userLikeLabel.textColor = customColorIt;
[userLikeLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:12]];
[userLikeView addSubview:userLikeLabel];
// image
UIImageView *likeImg = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"classifica_like_C.png"]];
likeImg.frame = CGRectMake(5, 0, 24, 24);
[userLikeView addSubview:likeImg];
// user like label
UILabel* userLikeCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 2, 20, 20)];
[userLikeCountLabel setText: [likesCount stringValue]];
userLikeCountLabel.backgroundColor = customColorGrey;
userLikeCountLabel.textColor = customColorIt;
[userLikeCountLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:12]];
[userLikeView addSubview:userLikeCountLabel];
// la mia bacheca like view
userLaMiaView = [[UIView alloc] initWithFrame:CGRectMake(10, 180, 300, 25)];
userLaMiaView.backgroundColor = [UIColor clearColor];
[sectionHeader addSubview:userLaMiaView];
// like label
UILabel* userLaMiaLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 150, 20)];
[userLaMiaLabel setText:NSLocalizedString(#"LA MIA BACHECA", nil)];
userLaMiaLabel.backgroundColor = [UIColor clearColor];
userLaMiaLabel.textColor = customColorGrey;
[userLaMiaLabel setFont:[UIFont fontWithName:#"DIN-Bold" size:10]];
[userLaMiaView addSubview:userLaMiaLabel];
// grey line view below la mia label
userGreyLineView = [[UIView alloc] initWithFrame:CGRectMake(10, 248, 280, 1.5)];
userGreyLineView.backgroundColor = [UIColor whiteColor];
[sectionHeader addSubview:userGreyLineView];
return sectionHeader;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 210;
}
That behavior is only common when the UITableViewStyle property of the table is set to UITableViewStylePlain. If you have it set to UITableViewStyleGrouped, the headers will scroll up with the cells.
This answer is taken from this question.
This solution works regardless of the number of headers.
create sectionHeader view in a new method and then add to the end:
self.tableView.tableHeaderView = sectionHeader;
if you have dynamic header view or single view select table style
Changing the UITableViewStyle from Plain to Grouped make the section header to scroll with the other cells.
If you write tableView.tableHeaderView = sectionHeader you will lose the actual table view header.
If you want to have table view header along with section headers you have to set UITableViewStyle property to UITableViewStyleGrouped.
Also if you want to calculate section header height automatically you can return UITableViewAutomaticDimension in heightForHeaderInSection method like this:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return UITableViewAutomaticDimension;
}
If you have a single header in the table then you can use tableHeaderView as below:
tableView.tableHeaderView = Header;
Or if you have multiple header in table than you need to use Group table instead of plain table.
Thanks

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

How can I know which view I am clicking? And why the button no reaction?

I am designing a viewcontroller which have several button bar, each bar canbe clicked and show a content view.like:
http://i.stack.imgur.com/m5V4Q.png
When I click the buttonbar, it's frame will expand bigger, and you can see the content in it(which is a button).
First, a bar button(320*30 size) and a contentView is a set, they are subviews of listview. And several listview makes the whole view.
Second, when I click it, the list view will expand from 320*30 to 320*180, the contentview in it will expand from 300*0 to 300 * 130(20 pixel of margin). and I set the clipsToBounds of contentview to Yes to make sure the content in contentview won't show when the frame height is 0.
Now,the click can show the contentview exactly as I want. But here is a problem: I can't click the button in it, and I tried to set their userInteractionEnabled to yes . And even I set the contentview as user-defined,which I set the view's userInteractionEnabled to yes and overwrite the touchbegin function to show a alertView. Those test are failed, no reaction found. And I checked and sure that there shouldn't be any view covered it.
What's the reason might be?
The code of setup the list view is:
NSArray *array = [titleArray objectAtIndex:i];
NSString *ShenSuoTitle = [array objectAtIndex:0];
NSString *ShenSuoContent = [array objectAtIndex:1];
UIView *listView = [[UIView alloc] initWithFrame:CGRectMake(0, totalHeight, 320, ShenSuoViewHeight)];
[listView setBackgroundColor:[UIColor blackColor]];
[listView setTag:[[NSString stringWithFormat:#"%d",(i+1)] intValue]];
[self.scrollView addSubview:listView];
totalHeight = totalHeight + 1 + ShenSuoViewHeight;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[titleView setBackgroundColor:[UIColor whiteColor]];
[titleView setTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
[listView addSubview:titleView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:[[NSString stringWithFormat:#"%d2",i+1] intValue]];
[btn setFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[btn addTarget:self action:#selector(reSetFrame:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:btn];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(5, 12, 30, 30)];
img.image = [UIImage imageNamed:#"list_ico.png"];
[img setTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
[titleView addSubview:img];
NSLog(#"img:%f,%f",img.frame.origin.y,img.frame.size.height);
UILabel *labTitle = [[UILabel alloc] initWithFrame:CGRectMake(35, 15, 100, 25)];
labTitle.textColor = [UIColor blackColor];
labTitle.backgroundColor = [UIColor clearColor];
labTitle.font = [UIFont boldSystemFontOfSize:15];
labTitle.text = ShenSuoTitle;
[titleView addSubview:labTitle];
NSLog(#"labTitle:%f,%f",labTitle.frame.origin.y,labTitle.frame.size.height);
//add a label for selected
UILabel *selectedLabel = [[UILabel alloc] initWithFrame:CGRectMake(214, 14, 86, 21)];
selectedLabel.textColor = [UIColor grayColor];
//selectedLabel.alpha = 0.75;
selectedLabel.backgroundColor = [UIColor clearColor];
selectedLabel.text = #"All";
selectedLabel.font = [UIFont boldSystemFontOfSize:15];
[selectedLabel setTag:[[NSString stringWithFormat:#"%d5",i+1] intValue]];
[titleView addSubview:selectedLabel];
NSLog(#"selectedLabel:%f,%f",selectedLabel.frame.origin.y,selectedLabel.frame.size.height);
UIView *content = [[UIView alloc] initWithFrame:CGRectMake(10, 10 + ShenSuoViewHeight, 300, 0)];
content.backgroundColor = [UIColor grayColor];
UILabel *testLa = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
testLa.text = #"Label";
UIButton *testBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testBut.frame = CGRectMake(0, 40, 100, 30);
testBut.titleLabel.textColor = [UIColor greenColor];
testBut.titleLabel.text = #"Button";
content.tag = [[NSString stringWithFormat:#"%d4",i+1] intValue];
[content addSubview:testLa];
[content addSubview:testBut];
content.clipsToBounds = YES;
[testLa release];
[listView addSubview:content];
[content release];
[labTitle release];
[img release];
[titleView release];
[listView release];
the code handle the click to make the list view expands is:
UIView *titleView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
titleView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:0.9];
UIView *listView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d",i+1] intValue]];
listView.frame = CGRectMake(0, totalHeight, 320, ShenSuoViewHeight);
totalHeight = totalHeight + 1 + 20 + 180 + ShenSuoViewHeight;
UIView *content = [self.view viewWithTag:[[NSString stringWithFormat:#"%d4",i+1] intValue]];
content.frame = CGRectMake(10, 10 + ShenSuoViewHeight, 300, 180);
UIImageView *img = (UIImageView*)[self.view viewWithTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
img.image = [UIImage imageNamed:#"list_ico_d.png"];
I'm not sure I follow your situation exactly, and some code would be nice. To answer your question though, the reason is most likely that the touch is being intercepted elsewhere. It may also be that you just forgot to connect an action method to the button. Try using breakpoints or NSLogs to see what methods are being triggered and which aren't.
Thanks a lot. It is due to my fault. The code of expanding the bar which setting the listview's frame is wrong, it should add in the expanded height. This leads the visual is ok but the clicking not be handled.
So this problem closed.

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