UIView won't recognize a subview - ios

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

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.

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

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.

Transparent Context in IOS?

I'm displaying an image in a UIScrollView but I want it to blend with a UIImageView which is laid behind the UIScrollView.
I'm currently trying to override - (void)drawRect:(CGRect)rect in a subclass of UIView created to display the image inside the scrollview, using either the method drawInRect:blendMode:alpha: or CGContextSetBlendMode but none worked. I think the reason is that the current context is opaque but I cannot find a workaround.
You can add UIImageView as a subclass of UIView (parent view of your scroll view).
You can change opacity of your UIImageView to match your criteria. If you just want a image as a background of your UIView you can set up background of your view and as a colour you can use [UIColor colorWithPatternImage:Your UIImage here].
Is it something you need?
//EXTENDED
I try it and both of the example below set the image as a background and when I scroll the image is not moving:
Example 1:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"newtons_cradle.jpg"]]];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[scrollView setContentSize:CGSizeMake(self.view.bounds.size.width, 1000.0f)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 300, 50)];
[label setBackgroundColor:[UIColor whiteColor]];
[label setText:#"Label Test Text"];
[scrollView addSubview:label];
[self.view addSubview:scrollView];
Example 2:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
[imageView setImage:[UIImage imageNamed:#"newtons_cradle.jpg"]];
[self.view addSubview:imageView];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[scrollView setContentSize:CGSizeMake(self.view.bounds.size.width, 1000.0f)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 300, 50)];
[label setBackgroundColor:[UIColor whiteColor]];
[label setText:#"Label Test Text"];
[scrollView addSubview:label];
[self.view addSubview:scrollView]
;

How to Center Text in UITableView.tableHeaderView?

I am adding a UILabel to the tableHeaderView but for some reason it is left aligned.
self.messageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.messageLabel setText:#"Nothing to display!"];
[self.tableView setTableHeaderView:self.messageLabel];
[self.tableView.tableHeaderView setBackgroundColor:[UIColor blueColor]];
[self.tableView.tableHeaderView setFrame:CGRectMake(320/2, 480/2, 100, 20)];
[self.messageLabel setCenter:self.tableView.tableHeaderView.center];
[label setTextAlignment:UITextAlignmentCenter];

Resources