Hello everyone I have a problem with my custom navigation bar.
I needed to create a custom navigation bar and this was to be used in several view controllers so i created it as a category for UIViewController and Used the following code for creating the customisation i needed.
- (void)setCustomLabel:(NSString *)labelText
{
UILabel *navigationLabel = [[UILabel alloc]initWithFrame:CGRectMake(60,10,40,40)];
[navigationLabel setBackgroundColor:[UIColor clearColor]];
navigationLabel.font = [UIFont fontWithName:#"Humanist 521 BT-Bold" size:15.0];
navigationLabel.font = [UIFont boldSystemFontOfSize:18.0];
navigationLabel.textColor = [UIColor whiteColor];
navigationLabel.text = labelText;
navigationLabel.shadowColor = [UIColor colorWithRed:241.0/255.0 green:241.0/255.0 blue:241.0/255.0 alpha:1.0];
navigationLabel.shadowOffset = CGSizeMake(0.0, -1.0);
[navigationLabel sizeToFit];
[self.navigationController.navigationBar addSubview:navigationLabel];
[navigationLabel release];
}
On first view there are 2 buttons signIn and Register when i click on signIn button it will take me to signIn View and when i click on register button it will take me to Register view.
I created 2 ViewControllers and set the navigationBarLabel in both views as Register and SignIn using the code :
[self setCustomLabel:#"REGISTER"];
and
[self setCustomLabel:#"SIGN IN"];
The views would have a title displayed as
and
and it does look this way when i first run the application and click on either register or signIn Button but if i click on any of the 2 buttons navigate to the register or signIn view and then i go back and click on the second button the navigation bar changes to
Please help me out i have been at this for a very long time i set the navigation bar in viewDidAppear and i have also tried setting it to nil
[self setCustomLabel:nil];
in viewWillDisappear and in viewDidDisappear . I am new to iPhone development help me ou
This is because you are adding the label to the navigation bar. Since it is the same navigation bar no matter how many views you go to, it just keeps adding new labels to the bar and leaves them there.
The way that I see it you have two options to fix this:
You can create a singular label one time and have it always on the navigation bar and just set its text in the method so that it always has the correct text and set it to hidden when you do not want it to be visible.
You can do the same thing you are doing, but add a tag to the label and before creating the new label, you can iterate through the NavigationBar's subviews and remove the old label by checking the tags.
You're not removing the label from the navigationbar when you're addign a new one.
Perhaps you should try calling [navigationLabel removeFromSuperview] on it when you wish to set a new one. (This means you probably have to store it in an #property)
example:
header file:
#property (assign, nonatomic) UILabel *navigationLabel;
Implementation file:
#synthesize navigationLabel
- (void)setCustomLabel:(NSString *)labelText {
if (self.navigationLabel) [self.navigationLabel removeFromSuperview];
self.navigationLabel = [[UILabel alloc]initWithFrame:CGRectMake(60,10,40,40)];
[self.navigationLabel setBackgroundColor:[UIColor clearColor]];
self.navigationLabel.font = [UIFont fontWithName:#"Humanist 521 BT-Bold" size:15.0];
self.navigationLabel.font = [UIFont boldSystemFontOfSize:18.0];
self.navigationLabel.textColor = [UIColor whiteColor];
self.navigationLabel.text = labelText;
self.navigationLabel.shadowColor = [UIColor colorWithRed:241.0/255.0 green:241.0/255.0 blue:241.0/255.0 alpha:1.0];
self.navigationLabel.shadowOffset = CGSizeMake(0.0, -1.0);
[self.navigationLabel sizeToFit];
[self.navigationController.navigationBar addSubview:self.navigationLabel];
[self.navigationLabel release];
}
Related
I have a tableview that shows up when I push a button. This tableview is added on top of my other UITableview. What I want to do now is when the tableview shows up. My background view is dimmed like when an alertview pops up. You can see an example over here.
I've tried this in code but it doesn't work.
_tableView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
Just adding answer with more description that my comment.
Say your Background tableView is _tableView and your pop up small tableView is _tableViewSmall
Create a UIView having same height and width as of _tableView. Do addSubView on your _tableView.
yourView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
Now add your _tableViewSmall doing addSubView on _tableView.
Hope this steps helps you to make it..
try this...
yourView.backgroundColor = [UIColor blackColor];
yourView.alpha = 0.40;
Make a blank black uiview on top of your table view and set its alpha as needed
in the storyboard.KEEP IT HIDDEN.
When alertview pops up show the view.
I am doing one iphone app. In this app i want to disable the back navigationleftbarbutton. I have used this below code for disable the navigationleftbarbutton but it is not working. Now i have created one view and i have added in uinavigationbar. But now i want to make that view in transparent view. And also how can i remove my customview(myview1) from the navigationbar. I have attached my code. please some body help me to solve my issue.
Disable Code:
self.navigationItem.leftBarButtonItem setEnabled:NO];
And This is my customview Code:
myview1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
myview1.backgroundColor = [UIColor redColor];
button = [[UIBarButtonItem alloc]initWithCustomView:myview1];
self.navigationItem.leftBarButtonItem = button;
How can i make myview is transparent view. And how can i remove mycustomview(myview1) from navigationbar. Thanks in advance.
you can hide navigation back button by writing code below:
self.navigationItem.leftBarButtonItem.hidden = YES;
and your view ie myview can be transparent by changing alpha value of the view
[myview setAlpha:0.3]; //alpha value ranges from 0 to 1
By settign alpha value 0.0 makes view transparent and increases transparency as u increases alpha value
Does anyone know how to customize the UISearchBar style? There are 3 default styles of the UISearchBar, but I want to customize other aspects of the search bar, such as its image, background. If there is no way to do that can I use the UITextField to replace the search bar's function?
Thanks All
As you discovered, you can customize a bar such as a search bar with the barStyle property and these two values:
UIBarStyleDefault
UIBarStyleBlack
Alternatively, you can choose a custom color for the bar using the tintColor property:
// use RGB values between 0.0 and 1.0
UIColor *barColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
searchBar.tintColor = barColor;
In either case, you can make the bar translucent by setting the translucent property to YES.
UISearchBar is a subclass of UIView, therefor you can simply add an UIView on top. (You just have to keep the area of the actual searchbar transparent.)
// Edit
It's easier if you add the image to the background (between the background and the search field, that is). To do this just add it and iterate over the subview to send the original background to the back (behind your image view).
searchBarOverlay = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SearchBarBack"]];
searchBarOverlay.frame = CGRectMake(-8, -2, 320, 48);
[searchBar addSubview:searchBarOverlay];
[searchBar sendSubviewToBack:searchBarOverlay];
for (UIView *v in [searchBar subviews]) {
if ([NSStringFromClass([v class]) isEqualToString:#"UISearchBarBackground"])
{
[searchBar sendSubviewToBack:v];
}
if ([NSStringFromClass([v class]) isEqualToString:#"UIImageView"] && v != searchBarOverlay)
{
[searchBar sendSubviewToBack:v];
}
}
For UISearchBarStyleProminent:
1) Be sure to check the "Translucent" box for the search bar in the Attributes Inspector.
2) Add the following to viewDidLoad:
self.navigationController.navigationBar.translucent = NO; // If navBar
self.searchDisplayController.searchBar.translucent = NO;
In order to get the minimal search bar to not be translucent I have put together a workaround.
1) Be sure to check the "Translucent" box for the search bar in the Attributes Inspector.
2) Add the following code to viewDidLoad:
self.navigationController.navigationBar.translucent = NO;
self.searchDisplayController.searchBar.translucent = NO;
self.searchDisplayController.searchBar.backgroundColor = [UIColor desiredColor];
3) A UIView needs to be added to the viewController. This view needs to be 20px heigh and should have the same color as the
searchBar.barTintColor.
How can I set custom font in UINavigationBar ? I need the tahoma font.
- (void)viewDidLoad{
self.title =#"My text";
}
Totally possible, if a little tricky to do. Once you've found the font you need (either one of the alternatives already shipped with iOS, or a TTF file that you've got the correct licensing for), just create a UILabel with the size, formatting, font etc and then add it to the navigation items for that bar (or, if you're doing this in a view controller, set the .navigationItem.titleView of that controller to your label).
For example, I have a view controller inside a UINavigationController. To change the label in the top to a custom font, I simply do:
//...I have already setup a UILabel called navLabel that has the same style as a
// default navigation bar title, but I've changed the .font property to my custom font
self.navigationItem.titleView = navLabel;
[navLabel release];
This code should work. In uiviewcontroller which presents your main ui:
- (void)viewDidLoad
{
[super viewDidLoad];
int height = navigationController.navigationBar.frame.size.height;
int width = navigationController.navigationBar.frame.size.width;
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
navLabel.backgroundColor = [UIColor clearColor];
navLabel.textColor = [UIColor whiteColor];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
navLabel.font = [UIFont boldSystemFontOfSize:15];
navLabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navLabel;
[navLabel release];
}
Note that resulting custom view has transparent background, so that you can add something more to your navigation bar with [navigationController.navigationBar addSubview:view]. This may be spinner in left corner of the bar or something else.
If you use custom view, you will not be able set the title with uiviewcontroller title anymore. You need to use the way available by your custom view.
Example:
((UILabel *)self.navigationItem.titleView).text = title;
removed dead ImageShack link
As you can see the view I need to change is the provided view to customize the tabbar order. I want to change the color of the navigation bar (displaying "Konfigurieren" which means "Configure"), I already found out how to change the color of the "More"-Navigation Controller, but not this one. Can anybody help me with that?
I think what you are looking for is this (to do when you create your navigation controller, typically in your app delegate):
UINavigationController *navigationController;
...
navigationController.navigationBar.tintColor = [UIColor blackColor];
Use int AppDelegate
tabBarController.moreNavigationController.navigationBar.tintColor = [UIColor blackColor];
Its Surely gonna work! :-)
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
Can be easier (use in tab bar delegate):
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers {
id modalViewCtrl = [[[tabBarController view] subviews] objectAtIndex:1];
if([modalViewCtrl isKindOfClass:NSClassFromString(#"UITabBarCustomizeView")] == YES)
((UINavigationBar*)[[modalViewCtrl subviews] objectAtIndex:0]).tintColor = [UIColor redColor];
}
There is an easy way to change all the navigation bar styles instead of changing each one separately.
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
Just set this code in one of your initial views. With this, your more navigation controller and the configuration navigation controller (that appears after clicking "Edit" in more navigation controller) get a different style.
Like this you can change its color to a different one or change the background image.
Hope this helps.
I was able to change the color of the Configure NavBar like this:
Create a new class that inherits from UITabBarController.
Implement this method:
-(void)beginCustomizingTabBar:(id)sender
{
[super beginCustomizingTabBar:sender];
// Get the new view inserted by the method called above
id modalViewCtrl = [[[self view] subviews] objectAtIndex:1];
if([modalViewCtrl isKindOfClass:NSClassFromString(#"UITabBarCustomizeView")] == YES)
{
UINavigationBar* navBar = [[modalViewCtrl subviews] objectAtIndex:0];
[navBar setBarStyle:UIBarStyleBlackTranslucent];
[navBar setTranslucent:YES];
}
}
Building off of the answer given by user486217, this may be even more defensively-coded:
id modalViewCtrl = [controller.view.subviews objectAtIndex:1];
if([modalViewCtrl isKindOfClass:NSClassFromStrin(#"UITabBarCustomizeView")] == YES) {
id navigationBar = [[modalViewCtrl subviews] objectAtIndex:0];
if ([navigationBar isKindOfClass:[UINavigationBar class]]) {
((UINavigationBar*)navigationBar).tintColor = [UIColor redColor];
}
}}
If you are looking for the standard colors (Gray, Black, White), you can set these values within xCode 5. Select the entire view controller, and select the attributes inspector. Under the attributes you will find a drop-down next to "Top Bar". There you can select various setting for color and opacity for the navigation bar controller.
Outlined below are a few screenshots. Hope this helps!