I have UISearchBar in UITableView as a table header. When I push the UISearchBar for start searching, this method is being triggered
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
for UISearchDisplayController.
But result is like that;
As you can see, there is no cursor, I can start typing and search, everything works fine. Also it's invisible only in iOS 7. However, with iOS 6.1 and iOS 7.1 Beta 3 I could see the cursor. So how can I make UISearchBar cursor visible or how can I add cursor in my UISearchBar?
Cursor in the search bar takes color from Search Bar -> View -> Tint Color property.
In your case, it is set to White color, so it becomes invisible.
try using with
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
self.navigationItem.titleView.tintColor = [UIColor blueColor];
}
hope this will help you
Try setting text field tint color using UIAppearance
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor: [UIColor darkGrayColor]];
If you want the cursor and cancel button to be different colors...
Start by setting the view's tint color (not the bar tint) in the storyboard editor, which will be applied to both the cursor and cancel button.
To make the cursor a different color, you need to do it programatically. The text field is nested a couple levels down in the searchBar's subviews. Use this setTextFieldTintColor helper function to traverse all of the subviews.
#IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
// set tint color for all subviews in searchBar that are of type UITextField
setTextFieldTintColor(to: UIColor.darkText, for: searchBar)
}
func setTextFieldTintColor(to color: UIColor, for view: UIView) {
if view is UITextField {
view.tintColor = color
}
for subview in view.subviews {
setTextFieldTintColor(to: color, for: subview)
}
}
The end result looks like this:
Try this it's only one line of code to solve your problem , Change cursor tintColor property white to blue.
searchBar.tintColor = [UIColor blueColor];
Hope this will help to someone .
Your cursor is white because your tintColor property on UISearchBar is set to white.
If you want Cancel btn to be white, but cursor to be black you can use:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black
Related
I am writing an app and I want to make a textfield background turn white.
So far this is my code:
- (void)viewDidLoad
{
UITextField *txtfield = UsernameTextField;
[txtfield setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.5] ];
txtfield.layer.borderColor = [UIColor blackColor].CGColor;
txtfield.layer.borderWidth = 1;
txtfield.borderStyle = UITextBorderStyleRoundedRect;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
But this code have a result like this:
and i tried this code , to increast alpha
[txtfield setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.8] ];
still the same
and also i see this post
iPhone UITextField background color
and i tried
but it still the same
Why textfield wont turn to white?
First thing first you should
[super viewDidLoad] should be the first statement in - (void)viewDidLoad after initialising parent child should do initialise.
Second
Please check the order of views put on view controller bring UsernameTextField to top of all views or you can call [self.view bringSubviewToFront:txtfield] in code
Third
Only by setting alpha to 1.0 you can see complete white background.
and finally is you still don't get it resolved then there is some changes you did in xib/storyboard view. So to reset just delete textfield and add it again.
In order to change background color it's important to set the border to None
txtfield.borderStyle = UITextBorderStyleNone;
Details: Changing background colour within a TextField in Interface Builder
iPhone UITextField background color
From Apple Doc:
When set, the image referred to by this property replaces the standard appearance controlled by the borderStyle property. Background images are drawn in the border rectangle portion of the text field. Images you use for the text field’s background should be able to stretch to fit.
I am setting the text color of all the labels in my app using UIAppearance. Yet the text color does not change.
Here is a sample of how i create the label
//show the loading message
MessageLabel *messageLabel = [[MessageLabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
messageLabel.text = #"\n\nLoading ...\n\n";
messageLabel.numberOfLines = 0;
messageLabel.textAlignment = NSTextAlignmentCenter;
[messageLabel sizeToFit];
self.tableview.backgroundView = messageLabel;
Here is how i set the text color
[[MessageLabel appearance] setTextColor:[UIColor blackColor]];
One note is that all these MessageLabel are BackgroundViews of UITableView
As arrteme mentioned, UILabel's textColor doesn't work with UIAppearance. I got around this by adding a UIAppearance compatible property that just wraps textColor. In Swift this would look something like
class MessageLabel: UILabel {
#objc dynamic var configurableTextColor: UIColor {
get {
return textColor
}
set {
textColor = newValue
}
}
...
}
and then to configure the appearance:
MessageLabel.appearance().configurableTextColor = .black
From the Documentation:
iOS applies appearance changes when a view enters a window, it
doesn’t change the appearance of a view that’s already in a window. To
change the appearance of a view that’s currently in a window, remove
the view from the view hierarchy and then put it back.
Referring to this, UIAppearance kind of doesn't really seem work with UILabel...
Since you're subclassing from UILabel, maybe it would make sense to set textcolor property initWithFrame: method on in your MessageLabel class?
Or another option, since you say these MessageLabel instances are used for UITableViewCell's background, maybe it would make sense to leave label's background clear and change background of cell itself, for example in tableView:willDisplayCell:forRowAtIndexPath: method in tableView's delegate?
If you instantiated a UILabel in code, you must manually set textColor property after you set the appearance.
messageLabel.textColor = [[UILabel appearance] textColor];
I am building iOS8 app. On my tableview controller, I am using self.navigationController.hidesBarsOnSwipe = YES, to hide the navigationBar on swipe up gesture. It is working nicely, but my statusBar becomes transparent and shows the table content underneath.
On storyboard, Status Bar are Top Bar are set to "Inferred"
I want to:
1. Keep my status bar opaque
2. Maintain the same color as the navigationBar
3. Table content scrolls underneath the statusBar
Thank you.
Here is a Swift solution:
First, change UITableViewController to UIViewController and add a tableView field.
Then, implement your viewDidLoad method as follows:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.frame = view.frame
view.addSubview(tableView)
let topBar = UIView(frame: UIApplication.sharedApplication().statusBarFrame)
topBar.backgroundColor = myDesiredColor
view.addSubview(topBar)
}
You can add a constraint to the top layout, by this scrolling content will not appear below the status bar.
Make a custom View.
UIView * statusBarView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
statusBarView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:statusBarView];
In case of UISegmentedControl, once a popover or alert is present, the Control dims to grey (desaturates the tint color)
I'am building my own UIControl subclass, which uses a UILabel as a subview
i want to dim (desaturate) the text color of the UILabel, same way as by UISegmentedControl or (UIButton...)
Look at the tintColor and tintAdjustmentMode properties on UIView (available since iOS 7) and the tintColorDidChange method.
If you override them in your custom view you can respond to being dimmed out.
As the iOS 7 UI Transitioning Guide says:
When an alert or action sheet appears, iOS 7 automatically dims the tint color of the views behind it. To respond to this color change, a custom view subclass that uses tintColor in its rendering should override tintColorDidChange to refresh the rendering when appropriate.
The solution may look like this :
- (void)tintColorDidChange {
self.titleLabel.textColor = self.tintColor;
}
While the accepted answer did help me, the result was that the dimmed color was applied to my control even when the screen was not dimmed. I fixed this in the following manner:
override func tintColorDidChange() {
switch tintAdjustmentMode {
case .Dimmed:
myLabel.textColor = UIColor.grayColor()
default:
myLabel.textColor = UIColor.blueColor()
}
}
This correctly applies a gray color to the control only if the screen is dimmed.
I have a UINavigationController based app. I can programatically set the title of the UINavigationBar from within my view controller's viewDidLoad method:
self.navigationItem.title = m_name;
Can I make it so that the title in the navigation bar is editable? IE. I want the user to be able to tap the title in the navigation bar, and to be able to edit it.
Is this possible? And if it is possible, does is it likely to meet Apple's Human Interface Guidelines? (This post suggests it will, but does not tell me how to implement it - Make UINavigationBar title editable)
Many thanks
Nathan
You are able to set a custom view for your title.
titleView
A custom view displayed in the center of the navigation bar when the
receiver is the top item.
#property(nonatomic, retain) UIView *titleView
Discussion
If this property value is nil, the navigation item’s title is
displayed in the center of the navigation bar when the receiver is the
top item. If you set this property to a custom title, it is displayed
instead of the title. This property is ignored if leftBarButtonItem is
not nil.
Custom views can contain buttons. Use the buttonWithType: method in
UIButton class to add buttons to your custom view in the style of the
navigation bar. Custom title views are centered on the navigation bar
and may be resized to fit.
If you were to place a UITextField in your titleView you could give it a clearColor background and set it to have no border. Wala you have a UINavigationBar Title you can tap and edit.
Ryan's solution is exactly right. In case anyone is having trouble implementing it, here is some sample code that should get you going. Just paste it into ViewDidLoad on any class with a navigationController.
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 22)];
textField.text = #"Insert Title Here";
textField.font = [UIFont boldSystemFontOfSize:19];
textField.textColor = [UIColor whiteColor];
textField.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = textField;
And if your project doesn't use ARC, make sure to release the textField like so:
UITextField *textField = [[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 22)]autorelease];
or like so:
[textField release];
Hope that helps.
Ryan's solution for swift
let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 22))
textField.text = "Title"
textField.font = UIFont.systemFontOfSize(19)
textField.textColor = UIColor.whiteColor()
textField.textAlignment = .Center
self.navigationItem.titleView = textField
Here is Ciprian's code in Swift 4 and corrected for iOS 12:
let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
textField.text = "Your Title"
textField.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
textField.textAlignment = .center
self.navigationItem.titleView = textField
You can put a text field right ON TOP the navigation bar, and set it hidden/not enabled., the use a gesture recognizer to detect a tap on the navigation bar and set the text field to enabled/ not hidden. Then in the textfield's did end editing method, set it to hidden again and set navigation bar's title to that of the textfield.
Add a hidden textfield over navigation bar, so on tapping, it will popup a keyboard and user can enter text into that.
Get the return button event, over there you can change your navigation bar text with newly added text. And hide the textfield again.
Enjoy Coding :)
Put a textField on the NavigationBar and set the textField background color with opacity to zero.