Cant Get UIView by Tag - ios

I'm not sure why I cant get the UIView by tag from above screen.
First pink bar is UIView with 55555 tag
First yellow container is UIView with 555552 tag
Label in first yellow container is 555553 tag
So my viewDidLoad as below
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationItem setTitle:#"Address"];
[self.view setBackgroundColor:[UIColor colorWithHexString:#"#efeff4"]];
UIView *addContainer1 = (UIView *)[self.view viewWithTag:555552];
[addContainer1 setBackgroundColor:[UIColor colorWithHexString:#"#ffffff"]];
UILabel *lblAddress1 = (UILabel *) [self.view viewWithTag:555553];
[lblAddress1 setNumberOfLines:0];
[lblAddress1 sizeToFit];
}
Below code working as expected. Changing whole view to grey color
[self.view setBackgroundColor:[UIColor colorWithHexString:#"#efeff4"]];
Get label in first yellow also working as expected.
UILabel *lblAddress1 = (UILabel *) [self.view viewWithTag:555553];
[lblAddress1 setNumberOfLines:0];
[lblAddress1 sizeToFit];
But to get the UIView with 555552 tag (yellow container) is not working.
UIView *addContainer1 = (UIView *)[self.view viewWithTag:555552];
[addContainer1 setBackgroundColor:[UIColor colorWithHexString:#"#ffffff"]];
What I'm missing here?

Related

Subview of UILabel disappears when the text was changed to Unicode string

I am seeing a strange behavior of UILabel and its subview on iOS development.
I want to have a overlay view on the UILabel and want to change only the text of the label.
It works well when the text is digits or alphabets.
However when I changed the text to a Unicode string, the subview disappears.
To reproduce the problem, I created a simple project which just have a label and a button.
The label has a "A" as the initial text.
When the button was tapped, the following method will be called and change the text of UILabel cyclically.
- (void)pushButton:(id)sender
{
if ([[_label text] isEqualToString:#"A"]) {
[_label setText:#"B"];
// add a subview on the label
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[view setBackgroundColor:[UIColor blueColor]];
[_label addSubview:view];
} else if ([[_label text] isEqualToString:#"B"]) {
[_label setText:#"C"];
} else if ([[_label text] isEqualToString:#"C"]) {
[_label setText:#"D"];
} else if ([[_label text] isEqualToString:#"D"]) {
[_label setText:#"\u2605"];
// after this, the subview disappears
} else {
[_label setText:#"A"];
// after this, the subview appears again
}
}
When I tap the button at the first time, the text is changed to "B" and the subview appears.
Second time and third time, the text is changed to "C" and "D" and the subview is still there.
Howerver the fourth time, the text is changed to "★" and the subview disappears.
Fifth time, the text is changed to "A" and the subview appears again.
In this code, I don't remove the subview and the new subview is created in every cycle.
However in any time the text was changed to "★", all these subviews disappear.
How can I keep the subview being appeared on the UILabel?
UILabel is not intended to have subviews. The solution is to make your blue rectangle a subview of your label's superview. You can position it so that it appears in front of your UILabel.
Thus, where you have this code:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[view setBackgroundColor:[UIColor blueColor]];
[_label addSubview:view];
You would instead say this:
UIView *sup = [_label superview];
UIView *view = [UIView new];
[view setBackgroundColor:[UIColor blueColor]];
CGRect r = CGRectMake(0,0,20,20);
r = [sup convertRect:r fromView:_label];
view.frame = r;
[sup addSubview: view];

UILabel not showing subview in iOS8

Currently I made a back Label for background and add UIButton on UILabel. It's showing in iOS 7 but in iOS 8 it's not showing . If I replace UILabel with UIView then It's working fine.
If I use UILabel in iOS 8 using this code For showing UILabel and subview UIButton
UILabel *downLabel = [[UILabel alloc]init];
downLabel.frame = CGRectMake(0, SCREEN_HEIGHT*0.92, SCREEN_WIDTH,SCREEN_HEIGHT*0.08 );
[downLabel setBackgroundColor:[UIColor colorWithRed:(66/255.f) green:(67/255.f) blue:(63/255.f) alpha:1]];
downLabel.userInteractionEnabled = YES;
[self.view addSubview:downLabel];
UIButton *downbtnobj = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.68,downLabel.frame.size.height/10,SCREEN_WIDTH*0.30,downLabel.frame.size.height/1.25)];
UIImage *btndownImage = [UIImage imageNamed:#"img 3.png"];
[downbtnobj setImage:btndownImage forState:UIControlStateNormal];
[downbtnobj addTarget:self action:#selector(bulletInButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[downLabel addSubview:downbtnobj];
Now You can see only UILabel is showing but button is not showing on UILabel.
One another this If I replace UILabel by UIView then It's showing perfect.
UIView *downLabel = [[UIView alloc]init];
downLabel.frame = CGRectMake(0, SCREEN_HEIGHT*0.92, SCREEN_WIDTH,SCREEN_HEIGHT*0.08 );
[downLabel setBackgroundColor:[UIColor colorWithRed:(66/255.f) green:(67/255.f) blue:(63/255.f) alpha:1]];
downLabel.userInteractionEnabled = YES;
[self.view addSubview:downLabel];
UIButton *downbtnobj = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.68,downLabel.frame.size.height/10,SCREEN_WIDTH*0.30,downLabel.frame.size.height/1.25)];
UIImage *btndownImage = [UIImage imageNamed:#"img 3.png"];
[downbtnobj setImage:btndownImage forState:UIControlStateNormal];
[downbtnobj addTarget:self action:#selector(bulletInButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[downLabel addSubview:downbtnobj];
In this code you can see I replace only UILabel by UIView. Now button is showing.
Can any help why subviews not showing on UILabel in iOS8
It's not clear what is different about labels in iOS 8, but if you call layoutIfNeeded on your label, that fixes the problem (it needs to be after you set the frame),
UILabel *downLabel = [[UILabel alloc]init];
downLabel.frame = CGRectMake(0, SCREEN_HEIGHT*0.92, SCREEN_WIDTH,SCREEN_HEIGHT*0.08 );
[downLabel layoutIfNeeded];
[downLabel setBackgroundColor:[UIColor colorWithRed:(66/255.f) green:(67/255.f) blue:(63/255.f) alpha:1]];
downLabel.userInteractionEnabled = YES;
[self.view addSubview:downLabel];
After Edit:
I also found out that if you set the text (any time after you create the label), then the button appears.
Very Simple way you need set clipsToBounds on your UILabel
downLabel.clipsToBounds = YES;

Programmatically created UIButton not visible

I have created a UIButton, added it to a view, which has then been added to a UIScrollView. This View also contains a UITextView which displays and is formatted correctly.
My code is below. Hopefully someone can help. The area where the button should be is clickable, and acts correctly, however the button is not visible. I even changed the tintColor to red, but I cannot see it.
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *billBlocker = *UIView initialized*
//The UITextView is a subview of `billBlocker`, as well as the `billBlockButton`
//added the UITextView here
UIButton *billBlockButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 5,292,30 )];
[billBlockButton setTitle:#"BillBlockerFrenzy" forState:UIControlStateNormal];
[billBlockButton setTintColor:[UIColor redColor]];
[billBlockButton addTarget:self action:#selector(segueToRegulationsGame) forControlEvents:UIControlEventTouchUpInside];
[billBlocker addSubview:billBlockButton];
[self.scrollView addSubview:billBlocker];
}
You should use cluster method when crate button:
UIButton *billBlockButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[billBlockButton setFrame:CGRectMake(0, 5,292,30 )];
...
This code should work. Make sure the scrollView frame and content size is set correctly and scrollView is added to self.view.
buttonWithType: is recommended method for creating the button, this is the only way you can specify button type.
When you use alloc init, I believe, you use some standard button type and if iOS change the standard also could change.
Probably the problem is you forgot the set outlet of scroll view i tried and its working like this
#property (strong,nonatomic) IBOutlet UIScrollView *sView; //scrollviews outlet
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//added the UITextView here
UIButton *billBlockButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 5,292,30 )];
[billBlockButton setTitle:#"BillBlockerFrenzy" forState:UIControlStateNormal];
[billBlockButton setBackgroundColor:[UIColor redColor]];
[billBlockButton addTarget:self action:#selector(nothing) forControlEvents:UIControlEventTouchUpInside];
[self.sView addSubview:billBlockButton];
}
Forgot to say you need to add your method instead of nothing method..
Hope it helps.
When we create the UIButton object programmatically, the default title color of UIButton is white.
So, set the title color of UIButton object.
[billBlockButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
iOS 7 or later will have transparent button. Unless you set text, you will not be able to see. Do following:
[[yourButton layer] setBorderColor:[UIColor blackColor].CGColor];
[[yourButton layer] setBorderWidth:2.0];
[yourButton setTitle:#"Hello" forState:UIControlStateNormal];

How to create gmail recipient format in iOS?

I need to create a view like below from the array of emails that I receive which also has the delete option.
Something similar to that of gmail's mail recipient except in my this should be scrollview. My main issue is to create the background with delete button which stretches based on the email length. My current approach is to use 3 images one for the beginning, one for the end with delete button and one in general for middle that will stretch. Is there any other or better way to do this?
NOTE:Need to support from iOS 5 and above
Here you go, I have created a SuperLabel for you though it might need some tweak.. But it will help you for sure..
SuperLabel.h
#import <UIKit/UIKit.h>
#interface SuperLabel : UIView
- (id)initWithFrame:(CGRect)frame andTitle:(NSString *)title;
#end
SuperLabel.m
#import "SuperLabel.h"
#define MAX_HEIGHT 25.0
#implementation SuperLabel
- (id)initWithFrame:(CGRect)frame andTitle:(NSString *)title
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//Design your label view
self.backgroundColor=[UIColor colorWithRed:.8 green:.8 blue:.8 alpha:1.0];
self.layer.borderColor=[[UIColor orangeColor] CGColor];
self.layer.borderWidth=1.0;
self.layer.cornerRadius=5.0;
//Add a Label
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, 100.0, MAX_HEIGHT)];
label.font=[UIFont systemFontOfSize:12.0];
label.textColor=[UIColor grayColor];
label.text=title;
[label sizeToFit];
[self addSubview:label];
//We will get resized frame after using sizeToFit after setting the text in the label.
CGRect rect=label.frame;
//Add a button
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:#"x" forState:UIControlStateNormal];
btn.titleLabel.font=[UIFont systemFontOfSize:12.0];
btn.titleLabel.textColor=[UIColor grayColor];
rect.origin.x=rect.origin.x+rect.size.width;
rect.origin.y=0;
rect.size=CGSizeMake(25.0, MAX_HEIGHT);
[self addSubview:btn];
[btn addTarget:self action:#selector(deleteMe:) forControlEvents:UIControlEventTouchDragInside];
btn.frame=rect;
//Change the whole frame of the label view
frame.size.height=MAX_HEIGHT;
frame.size.width=btn.frame.origin.x+btn.frame.size.width;
self.frame=frame;
}
return self;
}
-(IBAction)deleteMe:(id)sender{
[self removeFromSuperview];
}
#end
And finally add to your view by using following code..
SuperLabel *label=[[SuperLabel alloc] initWithFrame:CGRectZero andTitle:#"myemail#gmail.com"];
[self.view addSubview:label];
Cheers..

Changing the colour of UIView that is created dynamically

I am creating a UIView and its subviews (such as UITextfield, UIButton etc.) dynamically( by reading from JSON )
I am giving tags to all UIView and try to call by tag.
For instance I am trying to change the background colour of a view inside another view.
parent view's tag is 1, subview's is 11.
My sample code is as follows;
UIView *temp = [self.view viewWithTag:1];
if([[temp viewWithTag:11] isKindOfClass:[UIView class]])
{
[((UIView*)[temp viewWithTag:11]) setBackgroundColor:[UIColor blackColor]];
}
But there is no change, no effect.
Could you please help me on solving this.
#define PARENT_VIEW_TAG 1
#define CHILD_VIEW_TAG 11
UIView *parentView = [self.view viewWithTag:PARENT_VIEW_TAG];
for (UIView *vw in [parentView subviews]) {
if(vw.tag == CHILD_VIEW_TAG)
{
[vw setBackgroundColor:[UIColor blackColor]];
}
}

Resources