Changing the colour of UIView that is created dynamically - ios

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

Related

Adding subviews to an array of UIViews?

I have a scroll view on which I have a main UIView. This main UIView has an array of UIViews in it.
I want to add a test view over this child view
for (NSUInteger i = 0; i<self.maxPage+1; i++) {
CanvasBaseView *view =[[CanvasBaseView alloc]initWithFrame:CGRectMake(0, (self.canvas.height.floatValue+NAPKIN_PADDING)*i, self.canvas.width.floatValue,self.canvas.height.floatValue)];
[view addSubview:[self setDrawView:view]];
}
-(UIView*)setDrawView:(UIView *)main{
UIView *vw =[[UIView alloc]initWithFrame:main.frame];
[vw setBackgroundColor:[UIColor blackColor]];
return vw;
}
But when I do this only the first view has my new black background View. I tried it with Array of 2,6,10 and all of this only the first view is being populated
Even I tried this
UIView *vw = [UIView new];
vw.frame = main.frame;
[vw setBackgroundColor:[UIColor blackColor]];
return vw;
Unfortunately same result.
If I understand correctly you want to add a black overlay to each child view in your loop. In that case you should use the bounds property of the parent view, as child views are relative it's parents view.
Change this line:
UIView *vw =[[UIView alloc]initWithFrame:main.frame];
To this:
UIView *vw =[[UIView alloc]initWithFrame:main.bounds];

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

Cant Get UIView by Tag

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?

UISearchBar box color

I have a search bar and beside that i want a button with the same background color as the one in search bar. Does anyone know this color?
I think its #A4A4A4
Or you could try the Eyedropper tool in photoshop
Just customize the text field itself.
I am simply doing this and it works fine for me (iOS 7).
UITextField *txfSearchField = [_searchBar valueForKey:#"_searchField"];
txfSearchField.backgroundColor = [UIColor redColor];
This way you don't need to create an image, size it, etc...
without using private API's:
for (UIView* subview in [[self.searchBar.subviews lastObject] subviews]) {
if ([subview isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField*)subview;
[textField setBackgroundColor:[UIColor redColor]];
}
}

Changing UISearchBar search field background

I have a UISearchBar contained within a UIView and the UIView has been assigned to a UITableView's tableHeaderView. I'm trying to style the UISearchBar to look like this:
Out of the gate, this is what it looks like:
There are loads of SO questions and answers on how to style UISearchBar, its search field, etc., some with particular attention to backgrounds. Many of them involve traversing the UISearchBar's subviews, finding a UISearchBarBackground (or _UISearchBarSearchFieldBackgroundView) object, and setting its background color directly. I'm trying to find a supported API for doing this, however...
The setSearchFieldBackgroundImage:forState: method seems to have great potential, but this is what I get when I use a 1px white (or transparent) image using UIControlStateNormal:
Any ideas as to how to get this to work? I'd appreciate someone pointing me to a SO post that answers this, but I really do think I've read them all. Thanks a ton.
you can use the appearanceWhenContainedIn to target elements without traversing the view hierarchy. Something like:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor lightGrayColor]];
UPDATE:
As appearanceWhenContainedIn is now deprecated, use
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar self]]] <your setter here>];
I think the problem here is that when you use setSearchFieldBackgroundImage:forState:, the height of your image actually determines the height of the search field. The standard search field (at least as of iOS 8) is 28 points tall, so an image that height should give the effect you're after.
(Somewhat confusingly, this also means that a nine-part stretchable image generally won't look right—so if you were going for a look that wasn't just white on white, you need to use a three-part stretchable image that only stretches horizontally.)
Above iOS 5.0
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:#"YourBackground.png"]forState:UIControlStateNormal];
Please try following code
[[UISearchBar appearance]setBackgroundImage:[UIImage imageNamed:#“searchbar_bg.png"]];
for (UIView *searchbuttons in searchBar.subviews) {
if ([searchbuttons isKindOfClass:[UIButton class]]) {
UIButton *cancelbutton = (UIButton *)searchbuttons;
[cancelbutton setBackgroundImage:[UIImage imageNamed:#“canelBtn.png"] forState:UIControlStateNormal];
}
}
UITextField *searchField = [searchBar valueForKey:#"_searchField"];
searchField.background = [UIImage imageNamed:#"bg.png"];
You can use either the barTintColor or the backgroundImage property of the UISearchBar to directly accomplish what you are trying to do.
I reproduced your problem, and it seems to be solved by:
self.searchDisplayController.searchBar.barTintColor = [UIColor whiteColor];
or
self.searchDisplayController.searchBar.backgroundImage = [self imageWithColor:[UIColor whiteColor]];
P.S. If you are using the backgroundImage solution, you'll need to make the method imageWithColor as follows
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Tell me if it works!
iO9 Perfect works.
- (void)viewDidLoad
{
[super viewDidLoad];
[[self searchSubviewsForTextFieldIn:self.searchBar] setBackgroundColor: [UIColor redColor]];
}
- (UITextField*)searchSubviewsForTextFieldIn:(UIView*)view
{
if ([view isKindOfClass:[UITextField class]]) {
return (UITextField*)view;
}
UITextField *searchedTextField;
for (UIView *subview in view.subviews) {
searchedTextField = [self searchSubviewsForTextFieldIn:subview];
if (searchedTextField) {
break;
}
}
return searchedTextField;
}
#joneswah's answer is depricated in iOS 9.
For iOS 9 put this in your AppDelegate.m. I also added workaround for keyboard opening lag, when keyboard is initiated for the first time.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Remove lag on oppening the keyboard for the first time when clicked on any UITextField
UITextField *lagFreeField = [[UITextField alloc] init];
[self.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
//searchBar background color change
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]] setBackgroundColor:[UIColor greenColor]];//background color
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]] setTextColor:[UIColor blackColor];//text color
return YES;
}
As others suggested, you need to traverse through your searchbar's subviews and finding the UITextField instance. BUT, you need to set its layer.backgroundColor, as setting the backgroundColor property has no effect. I'll give an example:
for view in searchBar.subviews {
for subview in view.subviews {
if let searchField = subview as? UITextField {
searchField.layer.backgroundColor = UIColor.whiteColor().CGColor
}
}
}
This will give you what you need.
My answer stably works on ios 5 and above, including this 10. Without hacks and KVO, by Apple Way
The following code will achieve the result.
_sbAddress.barTintColor = [UIColor whiteColor];
for (UIView *subView in _sbAddress.subviews) {
for(id field in subView.subviews){
if ([field isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)field;
[textField setPlaceholder:#"Search"];
}
}
}
OR
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setPlaceholder:#"Search"];

Resources