UINavigationBar not transparent on first load - objective-c

I have a view that needs a transparent UINavigationBar. I have some code that does just that. However, when the view first loads, I get the normal white UINavigationBar. It's only after I press back and go into the view again, that it's transparent as it should be. What am I doing wrong?
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"View will appear");
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[self.navigationController.navigationBar
setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
[self setNeedsStatusBarAppearanceUpdate];
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
self.navigationItem.titleView.hidden = YES;
}

Related

how to change UISearchBar textfield background color and text color in ios8

I used below code for changing UISearchBar textfield background color.
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{
NSForegroundColorAttributeName : [UIColor redColor],
NSFontAttributeName : [UIFont systemFontOfSize:15]
}];
but it does not work for me, can any one give solution.
thanks in advance
Try this:
UITextField *searchField = [self.searchBar valueForKey:#"searchField"];
// To change background color
searchField.backgroundColor = [UIColor blueColor];
// To change text color
searchField.textColor = [UIColor redColor];
// To change placeholder text color
searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Some Text"];
UILabel *placeholderLabel = [searchField valueForKey:#"placeholderLabel"];
placeholderLabel.textColor = [UIColor grayColor];
Just try this code
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor grayColor]];
OR
Try this
- (void)viewDidLoad
{
[super viewDidLoad];
//change the background color
[[self searchViewForTextFieldBg:self.searchTextfield] setBackgroundColor:[UIColor grayColor]];
//change the textcolor
self.searchTextfield.textColor =[UIColor greenColor];
}
- (UITextField*)searchViewForTextFieldBg:(UIView*)view
{
if ([view isKindOfClass:[UITextField class]]) {
return (UITextField*)view;
}
UITextField *searchTextField;
for (UIView *subview in view.subviews) {
searchTextField = [self searchViewForTextFieldBg:subview];
if (searchTextField) {
break;
}
}
return searchTextField;
}

How to make status bar translucent?

i need help in making status bar translucent. In some parts of app its working fine, and in other its not.
This is how status bar is coming if i show navugation bar.
this is how is showing when i remove the navigation bar.
I need to show the navigation bar but status bar should be translucent. The background image is pushed down with the navigation bar.I have set the background image programmatically making a base class.
-(void)addBackGroundImage{
self.backGroundImage=[[UIImageView alloc] init];
self.backGroundImage.translatesAutoresizingMaskIntoConstraints=NO;
[self.backGroundImage setImage:[UIImage imageNamed:#"background"]];
self.backGroundImage.contentMode=UIViewContentModeScaleAspectFill;
[self.view addSubview:self.backGroundImage];
[self.view sendSubviewToBack:self.backGroundImage];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[_backGroundImage]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_backGroundImage)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(0)-[_backGroundImage]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_backGroundImage)]];
AppDelegate *objAppDelegate=[[UIApplication sharedApplication] delegate];
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 20)];
view.backgroundColor=[UIColor clearColor];
[view setAlpha:0.2];
}
-(void)updateBackgroundImage:(UIImage *)bgImage{
[self.backGroundImage setImage:bgImage];
}
It is actually what you need to completely remove default header background (statusBar + navigationBar) and set your own:
// ViewController.m
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.tintColor = [UIColor yourColor]; // Set your tint color
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.opaque = NO;
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Your_Background_Image"]];
[self.view addSubview:bgView];

How to set a tableview cell's border and color when selected

I have the following code that sets sets a red border around a custom table view cell with white in the middle when it is selected.
- (void)awakeFromNib
{
self.nameLabel.highlightedTextColor = [UIColor whiteColor];
// Set selected background view
UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
backgroundView.layer.borderColor = [[UIColor redColor] CGColor];
backgroundView.layer.borderWidth = 10.0f;
self.selectedBackgroundView = backgroundView;
// Set the content view
CGRect frame = CGRectMake(self.bounds.origin.x+5, self.bounds.origin.y+5, self.bounds.size.width-10, self.bounds.size.height-10);
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:frame];
[self addSubview:myImageView];
self.imageView.contentMode = UIViewContentModeScaleAspectFill ;
self.imageView.clipsToBounds = YES;
[self.contentView addSubview:self.imageView];
}
I want the opposite: how would you create a cell that has red with a white border / padding?
You can use
[cell.contentView.layer setBorderColor:[UIColor redColor].CGColor];
[cell.contentView.layer setBorderWidth:2.0f];
Hope this will help you
try this in custom cell there is a method - (void)setSelected:(BOOL)selected animated:(BOOL)animated use this to change the state of selected and deselect state for example
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if(selected)
{
self.contentView.layer.cornerRadius = 10.0f;
self.contentView.layer.borderWidth = 5.0f;
self.contentView.layer.masksToBounds = YES;
self.contentView.backgroundColor = [UIColor redColor];
self.contentView.layer.borderColor = [UIColor whiteColor].CGColor;
}
else
{
self.contentView.layer.cornerRadius = 10.0f;
self.contentView.layer.borderWidth = 5.0f;
self.contentView.backgroundColor = [UIColor whiteColor];
self.contentView.layer.borderColor = [UIColor redColor].CGColor;
}
// Configure the view for the selected state
}
and also set customCell.selectionStyle = UITableViewCellSelectionStyleNone; for cell while creating the cell
why don't you try doing just the opposite such as this
UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
backgroundView.layer.borderColor = [[UIColor whiteColor] CGColor];
backgroundView.layer.borderWidth = 10.0f;
self.selectedBackgroundView = backgroundView;
// Set the content view
CGRect frame = CGRectMake(self.bounds.origin.x+5, self.bounds.origin.y+5, self.bounds.size.width-10, self.bounds.size.height-10);
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:frame];
myImageView.backgroundcolor=[UIColor redColor];
[self addSubview:myImageView];
self.imageView.contentMode = UIViewContentModeScaleAspectFill ;
self.imageView.clipsToBounds = YES;
[self.contentView addSubview:self.imageView];

self.navigationItem.titleView cutting off UIBarButtonItem after rotation

I use a self.navigationItem.titleView and I set two UIButtons as the self.navigationItem.rightBarButtonItems in the UINavigationBar. I have achieved this in my navigationBar like so
Whenever I rotate the view controller landscape and then back, the uibarbutton item gets cut off like so
The code I use to set the titleView is this:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.numberOfLines = 2;
titleLabel.font = [UIFont boldSystemFontOfSize:13.0f];
titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = self.tabTitle;
self.navigationItem.titleView = titleLabel;
I have no idea why this is happening and I wonder if if any of you know how to fix this.

UINavigationBar styling issue?

I have a UINavigationBar I am having style issues. To start out, I use this code (a class inside my navigation controller) to style my UINavigationBar.
#import...
#implementation UINavigationBar (UINavigationBarCustomDraw)
- (void) drawRect:(CGRect)rect {
[self setTintColor:[UIColor colorWithRed:0.9f green: 0.9f blue:0.9f alpha:1]];
if ([self.topItem.title length] > 0 && ![self.topItem.title isEqualToString:#"Back to ..."]) {
[[UIImage imageNamed:#"UINavigationBar_background.png"] drawInRect:rect];
CGRect frame = CGRectMake(0, 0, 320, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
[label setBackgroundColor:[UIColor clearColor]];
label.font = [UIFont boldSystemFontOfSize: 20.0];
label.shadowColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor colorWithRed:(70.0 / 255.0) green:(70.0 / 255.0) blue:(70.0 / 255.0) alpha: 1];
label.text = self.topItem.title;
self.topItem.titleView = label;
} else {
//[[UIImage imageNamed:#"login_button.png"] drawInRect:rect];
self.topItem.titleView = [[[UIView alloc] init] autorelease];
}
}
#end
#implementation...
Here's my two issues:
When I apply this code in another document with different form and styles, it changes everything. Why is this happening?
When moving from one view to another, the textColor changes to white. It shows great on start up and when I go back in the navigation controller it works fine.
Thanks in advance!
PS: If you need any more code, please ask!