Why is nothing displaying in my UIView? - ios

I have following code that adds a UIView and then adds a UIImageView and UILabel, the UIView display correctly but nothing i have put in side the view is showing. I have looked everywhere but i cannot find an answer. The UIView is presented under certain conditions and when it is tapped it dissapears.
Here is my code.
UIView *actionView = [[UIView alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[actionView addGestureRecognizer:singleFingerTap];
actionView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
actionView.layer.cornerRadius = 5;
actionView.layer.masksToBounds = YES;
[self.view addSubview:actionView];
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
actionText.text = #"Hello";
actionText.textColor = [UIColor redColor];
actionText.textAlignment = NSTextAlignmentCenter;
[actionText setTextColor:[UIColor redColor]];
[actionText setBackgroundColor:[UIColor clearColor]];
[actionText setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
actionText.Frame = CGRectMake(20, 180, 280, 165);
[actionView addSubview:actionText];
UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"trash-icon.png"]];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(109, 273, 40, 40);
[actionView addSubview:blockView];

Change your frame positions like..
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 40)];
actionText.text = #"Hello";
actionText.textColor = [UIColor redColor];
actionText.textAlignment = NSTextAlignmentCenter;
[actionText setTextColor:[UIColor redColor]];
[actionText setBackgroundColor:[UIColor clearColor]];
[actionText setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
[actionView addSubview:actionText];
UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"trash-icon.png"]];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(109,70, 40, 40);
[actionView addSubview:blockView];

When you set the frame of a view, it is important to remember that the origin relates to the coordinate system of the superview it is added to.
So when you set the frame of your label to:
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
the origin of x = 20, y = 180 end up being the origin WITHIN the actionView. Now this is your problem because your actionView is only 165px tall, and you are telling the actionText subView that it's y origin is 180, which is well outside the bottom of the actionView.
for your actionText, you should allocate it as follows:
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 165)];
and for your blockView, something like:
blockView.frame = CGRectMake(89,93, 40, 40);

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.

IOS, setting up Gesture Tap on UILabel thats inside a UIScrollView

I have a UIScrollView and inside this Scroll View i have some controls. Specifically a label thats underlined and i have setup Tap Gesture for the UILabel but doesn't seem to fire the methods that the Tap Gesture is bound to.
Here is the code:
-(void)setupUserNameControls:(UIScrollView*)scroll{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 23, 70, 16)];
label.textColor = [UIColor colorWithRed:59.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
label.text = #"First Name";
label.font = [UIFont fontWithName:#"Helvetica" size:14];
[scroll addSubview:label];
UIView *textView = [[UIView alloc] initWithFrame:CGRectMake(88, 16, 215, 28)];
textView.backgroundColor = [UIColor colorWithRed:60.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
[scroll addSubview:textView];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(15, 55, 70, 16)];
label2.textColor = [UIColor colorWithRed:59.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
label2.text = #"Last Name";
label2.font = [UIFont fontWithName:#"Helvetica" size:14];
[scroll addSubview:label2];
UIView *textView2 = [[UIView alloc] initWithFrame:CGRectMake(88, 48, 215, 28)];
textView2.backgroundColor = [UIColor colorWithRed:60.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
[scroll addSubview:textView2];
UIImageView *imageViewSep = [[UIImageView alloc] initWithFrame:CGRectMake(10, 92, 300, 2)];
UIImage *imageSep = [UIImage imageNamed:#"seperator.png"];
imageViewSep.image = imageSep;
[scroll addSubview:imageViewSep];
UIView *viewImage = [[UIView alloc] initWithFrame:CGRectMake(10, 108, 58, 58)];
viewImage.backgroundColor = [UIColor colorWithRed:132.0f/255 green:132.0f/255 blue:132.0f/255 alpha:1.0f];
[scroll addSubview:viewImage];
UIImageView *imageViewUser = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 48, 48)];
UIImage *imageUser = [UIImage imageNamed:#"defaultuser.jpg"];
imageViewUser.image = imageUser;
[viewImage addSubview:imageViewUser];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:#"Click to select your user image"];
[attributeString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:(NSRange){0,[attributeString length]}];
UILabel *labelPickImage = [[UILabel alloc] initWithFrame:CGRectMake(88, 126, 215, 16)];
labelPickImage.numberOfLines = 0;
labelPickImage.textColor = [UIColor colorWithRed:59.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
labelPickImage.attributedText = attributeString;
labelPickImage.font = [UIFont fontWithName:#"Helvetica" size:14];
[scroll addSubview:labelPickImage];
UITapGestureRecognizer *userImageTextTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(userImageSelectTap:)];
[userImageTextTap setNumberOfTouchesRequired:1];
[userImageTextTap setNumberOfTapsRequired:1];
[labelPickImage addGestureRecognizer:userImageTextTap];
}
-(void)userImageSelectTap:(UIGestureRecognizer *)sender{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}else{
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *) Picker {
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
This is how i set up the UIScrollView and pass it in:
UIScrollView *userInfoScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 108, screenRect.size.width, screenRect.size.height - 40 - viewTop.bounds.size.height - viewTopBorder.bounds.size.height)];
[userInfoScrollView setBackgroundColor:[UIColor colorWithRed:228.0f/255 green:228.0f/255 blue:228.0f/255 alpha:1.0f]];
userInfoScrollView.userInteractionEnabled = YES;
[self.viewUser addSubview:userInfoScrollView];
make sure the label stays in front of the scrollView.
[scroll bringSubviewToFront:labelPickImage];
Also,
labelPickImage.userInteractionEnabled = YES;

Centering UI elements in middle of screen programmatically

I'm trying to do something fairly simple, but just can't manage to make it happen. I have an UIImage followed by two UILabels, each on on top of the other, that I want to center in the middle of a UIView.
(empty space)
UIImage
UILabel
UILabel
(empty space)
This is what I have so far:
UIView* baseView = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] applicationFrame].size.width,[[UIScreen mainScreen] applicationFrame].size.height)];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 175, 175)];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
[imageView setCenter:CGPointMake(baseView.frame.size.width / 2, baseView.frame.size.height / 2)];
[label1 setCenter:CGPointMake(baseView.frame.size.width / 2, baseView.frame.size.height / 2)];
[label2 setCenter:CGPointMake(baseView.frame.size.width / 2, baseView.frame.size.height / 2)];
[baseView addSubview:imageView];
[baseView addSubview:label1];
[baseView addSubview:label2];
[self addSubview:baseView];
But it doesn't seem to be working. All the elements are over each other. Any ideas?
Have it a try.
I add an container view to hold the imageView and the two labels, then center the container view in baseView. Set backgroundColor of container to clear color so we can't see it.
UIView* baseView = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] applicationFrame].size.width,[[UIScreen mainScreen] applicationFrame].size.height)];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 175, 175)];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
UIView * container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 175+50+50)] ;
container.backgroundColor = [UIColor clearColor] ;
[imageView setCenter:CGPointMake(container.bounds.size.width/2, imageView.bounds.size.height/2)];
[label1 setCenter:CGPointMake(imageView.center.x, imageView.bounds.size.height + label1.bounds.size.height / 2)];
[label2 setCenter:CGPointMake(imageView.center.x, label1.frame.origin.y + label1.bounds.size.height + label2.bounds.size.height / 2)];
container.center = CGPointMake(baseView.bounds.size.width/2, baseView.bounds.size.height/2) ;
[container addSubview:imageView];
[container addSubview:label1];
[container addSubview:label2];
[baseView addSubview:container] ;
[self addSubview:baseView];

Avoid my section header to be redraw in viewForHeaderInSection

I have a UITableView with only 1 section title that contains labels when i reload data some of the label of the section may be hidden. But when I do reload data they are redraw in section I just wanted to know if there is some sort of dequeueReusableCellWithIdentifier: to avoid this like for the cell but for secions.
Here is the code
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 36)];
/* Create custom view to display section header... */
UILabel *labelSeq = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 30, 18)];
[labelSeq setFont:[UIFont boldSystemFontOfSize:12]];
labelSeq.text = #"SEQ";
UILabel *labelSup = [[UILabel alloc] initWithFrame:CGRectMake(50, 2, 330, 18)];
[labelSup setFont:[UIFont boldSystemFontOfSize:12]];
labelSup.text = #"SUPPLIER";
UILabel *labelMod = [[UILabel alloc] initWithFrame:CGRectMake(390, 2, 150, 18)];
[labelMod setFont:[UIFont boldSystemFontOfSize:12]];
labelMod.text = #"MODEL";
UILabel *labelFinish = [[UILabel alloc] initWithFrame:CGRectMake(550, 2, 150, 18)];
[labelFinish setFont:[UIFont boldSystemFontOfSize:12]];
labelFinish.text = #"FINISH";
UILabel *labelQty = [[UILabel alloc] initWithFrame:CGRectMake(710, 2, 30, 18)];
[labelQty setFont:[UIFont boldSystemFontOfSize:12]];
labelQty.text = #"QTY";
UILabel *labelSale = [[UILabel alloc] initWithFrame:CGRectMake(750, 2, 125, 18)];
[labelSale setFont:[UIFont boldSystemFontOfSize:12]];
labelSale.textAlignment = UITextAlignmentRight;
labelSale.text = #"SALE PRICE";
labelSale.tag = 100;
UILabel *labelCost = [[UILabel alloc] initWithFrame:CGRectMake(885, 2, 125, 18)];
[labelCost setFont:[UIFont boldSystemFontOfSize:12]];
labelCost.textAlignment = UITextAlignmentRight;
labelCost.text = #"COST";
labelCost.tag = 200;
UILabel *labelDesc = [[UILabel alloc] initWithFrame:CGRectMake(50, 17, 700, 18)];
[labelDesc setFont:[UIFont boldSystemFontOfSize:12]];
labelDesc.text = #"DESCRIPTION";
UILabel *labelUnit = [[UILabel alloc] initWithFrame:CGRectMake(750, 17, 125, 18)];
[labelUnit setFont:[UIFont boldSystemFontOfSize:12]];
labelUnit.textAlignment = UITextAlignmentRight;
labelUnit.text = #"UNIT PRICE";
labelUnit.tag = 300;
UILabel *labelProfit = [[UILabel alloc] initWithFrame:CGRectMake(885, 17, 125, 18)];
[labelProfit setFont:[UIFont boldSystemFontOfSize:12]];
labelProfit.textAlignment = UITextAlignmentRight;
labelProfit.text = #"PROFIT";
labelUnit.tag = 400;
[view addSubview:labelSeq];
[view addSubview:labelSup];
[view addSubview:labelMod];
[view addSubview:labelFinish];
[view addSubview:labelQty];
[view addSubview:labelSale];
[view addSubview:labelCost];
[view addSubview:labelDesc];
[view addSubview:labelUnit];
[view addSubview:labelProfit];
[view setBackgroundColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]]; //your background color...
_headerView = view;
return view;
}
Add a property to your class to hold on to that UIView. Then in -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section, just check to see if that property is nil or not. If it's nil, create it and set that property to it, otherwise just use the UIView saved in the property. If it requires updating it some, then you can do that at that point. It looks like all your content is static, so you should never need to update the view at all after the first time you create it.

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

Resources