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.
Related
I try to set image on navigation bar. when we pressed more button in tabbar..The default colour is blue.i wanna set image and chnge the colour of more label from white to green..Please help me
My code is given below, but it does not work
Appdelegte.m
tabMenu.moreNavigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[Uiimage imagewithnamed :#"nav.png"]];
tabMenu.moreNavigationController.delegate =self;
tabMenu.delegate = self;
You can do it like this :
[[[self.tabBarController moreNavigationController] navigationBar] setBackgroundImage:[UIImage imageNamed:#"myImage"] forBarMetrics:UIBarMetricsDefault];
However You can get instance of this "More" ViewController using...
UIViewController *moreViewController = tabBarController.moreNavigationController.topViewController;
moreViewController.view property contains UITableView and we can use it.
For example : To Add tableHeaderView :
UITableView *moreTableView = (UITableView*)moreViewController.view;
moreTableView.tableHeaderView = myOwnCustomView;
For more Reference you can check this Thread on Apple : moreNavigationController
I see that few apps are extending keyboard but I would like to know how they do it.
Here are 2 examples.
Textastic &
Prompt
Now I know that I can add inputAccessoryView to UITextView but it still has small thin line that separates keyboard from UIToolbar like on image bellow.
How they do it? Extending UIWindow that holds keyboard or in some other way?
Update 1 with answer:
So I have used solution that Tyraz wrote.
Subclass UIToolbar
Instead of image I have used UIView with background color same as the finishing color of the keyboard gradient and with UIViewAutoResizingMaskFlexibleWidth so that it covers keyboard when rotated, with height of 3 pixels
Here is the code for the subclassed UIToolbar
- (void)didMoveToSuperview {
[self.separatorHideView removeFromSuperview];
CGRect seperatorRect = CGRectMake(self.frame.origin.x,
self.frame.size.height,
self.frame.size.width,
3.0);
self.separatorHideView = [[UIView alloc]];
self.separatorHideView.backgroundColor = [UIColor colorWithRed:0.569 green:0.600 blue:0.643 alpha:1.000];
self.separatorHideView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self addSubview:self.separatorHideView];
}
Update 2: Here is code how I'm adding it to UITextView and what color I'm using for tint.
I'm adding it to the UITextView in viewDidLoad with following code
CustomToolbar *accessoryToolbar = [[CustomToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 38)];
accessoryToolbar.tintColor = [UIColor colorWithRed:0.569 green:0.600 blue:0.643 alpha:1.000];
editor.inputAccessoryView = accessoryToolbar;
And this is how it looks like with solution applied to it
On iOS 7 you can create an inputAccessoryView to match keyboard style easily:
[[UIInputView alloc] initWithFrame:<#frame#>
inputViewStyle:UIInputViewStyleKeyboard];
I would try to use a inputAccessoryView plus a second view that sits on top of the separator line and "fills the gap".
Once the inputAccessoryView is added to the keyboard (overwrite the didMoveToSuperview method in your accessory UIView subclass to get notified when this happens), add it to the inputAccessoryView's superview.
Should be something like that in your accessory UIView subclass:
- (void)didMoveToSuperview {
[self.separatorHideView removeFromSuperview];
CGRect seperatorRect = CGRectMake(self.frame.origin.x,
self.frame.origin.y + self.frame.size.height,
self.frame.size.width,
2.0);
UIImage *gapGradient = [UIImage imageNamed:#"GapGradient"];
self.separatorHideView = [[UIImageView alloc]initWithImage:gapGradient];
self.separatorHideView.frame = seperatorRect;
[self.superview addSubview:self.separatorHideView];
}
I would also overwrite setFrame in your accessory UIView subclass to update the frame of the gapView in case the frame of the keyboard is changed.
I have a dark background with a UITableView on top of it. By default the section index is semi-transparent with a dark text colour. I'd like to change the text colour for the section index to the same colour as I have made the UITableViewCell title label. I have read around a bit and it seems you have to subclass the UITableView? How do I do this?
Since iOS 6 you have the possibility to do it like:
searchTable.sectionIndexColor = [UIColor blackColor];
To solve this I used the following in viewDidAppear:
for (UIView *subView in [self.view subviews])
{
if ([[[subView class] description] isEqualToString:#"UITableViewIndex"])
{
[subView performSelector:#selector(setIndexColor:) withObject:[UIColor whiteColor]];
}
}
Since it's not documented, it has to be through a selector.
As of iOS 6.0 there are two methods that allow you to change the color of the section indexes and the background shown when you drag the scrubber.
if ([tableView respondsToSelector:#selector(setSectionIndexColor:)]) {
tableView.sectionIndexColor = ... // some color
tableView.sectionIndexTrackingBackgroundColor = ... // some other color
}
Of course this will only execute if the device has 6.0+. With any older iOS, nothing will change.
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!