How do I remove the black border of a textfield? - ios

I made a textfield in IB with the following settings:
Borderstyle: RoundRect
View's BG Color: Black, Opacity: 15%
The result is that the inside of the field is 15%, but there's a very thin visible border that isn't, and I want that removed. I've tried doing it in code like this:
textField.borderStyle = UITextBorderStyle.None
textField.layer.cornerRadius = 10
textField.layer.borderColor = UIColor(red:1.0,green:1.0,blue:1.0,alpha:0.15).CGColor
But this just puts the border on the inside covering the actual textfield.
Textfield:

Swift 3 / Swift 4
The simplest approach to achieving a border-free textfield is to change the style of the textfield.
Steps
Make a IBOutlet reference to the textfield.
Set the textfield's
border style to none.
Example Code
// Reference
#IBOutlet var tf: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Email TextField: no border
tf.borderStyle = .none
}
The Result

Try to add this:
textField.layer.borderWidth = 0
Additionally, from the screenshot, it seems that textField.layer.cornerRadius = 10 is ignored, make sure that your textField property is connected to the actual UITextField.

Swift3
passwordTF.layer.borderColor = UIColor.clear.cgColor
For me its working as well as
mobileNumberTF.borderStyle = .none

I've tried several ways to remove the broder by programmatically and other suggested ways but later figured out this is the only way it removes the border from UITextField. Just select in interface builder, Border Style option

Try this one
self.nameTextField.borderStyle = UITextBorderStyleNone;

Related

iOS 11 UISearchBar background color

I understand that this question has been asked many, many times on SO. However, as Apple does best, with the release of iOS 11, they seem to have made a seemingly unnecessary change to the UISearchBar, specifically it's view hierarchy.
In further, the "text field" of a search bar is no longer accessible in the search bar's subviews, causing all of the previous solutions to "access" and change the background color of the text field, or any property of the text field for that matter.
Does anyone know how to actually adjust the background color of a search bar in iOS 11?
FYI:
I am specifically talking about the color behind the text... which now as of 11 defaults to white unless you specify the search bar style to be minimal.
UPDATE 1:
Since my posting of this question, I still have not found a valid or really any real solution to this issue. The closest I have seem to come is to dive deep into the appearance for instance properties
[[UISearchBar class] appearanceWhenContainedInInstancesOfClasses:(nonnull NSArray<Class<UIAppearanceContainer>> *)]
of the UISearchBar. Playing around with the found UITextField via methods such as the following:
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;
you can begin drawing a new background view to be placed behind the view. However, the issues I had found too tedious to pursue further were drawing the a view with the correct frame / bounds to mimic exactly the original background.
Hopefully someone can find the actual solution to this problem. Nice miss apple...
I think you may be looking for this, right? But I've it in Swift :(
#IBOutlet weak var sbSearchBar: UISearchBar!
if let textfield = sbSearchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
textfield.backgroundColor = UIColor.yellow
}
Here is result:
This Swift code changes the background color of the text field:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// background color of text field
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .cyan
}
This is the result
let searchBar = UISearchBar(frame: CGRect())
let searchField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField
let searchBarBackground: UIView? = searchBar.value(forKey: "background") as? UIView
// searchBarBackground?.removeFromSuperview()
if searchField != nil {
var frame = searchField?.frame
frame?.size.height = 30
searchField?.frame = frame!
searchField?.backgroundColor = .yellow
}
searchBar.barTintColor = .red
searchBar.delegate = self
searchBar.backgroundColor = .green
Runtime Views Hierarchy
If we set background colors for UISearchBar with code above, we'll see the colored subviews as follow images(click links to see).

backgroundColor for Superview of UISearchBar subviews
We can see the Class Name of green view is UISearchBar in Object inspector.
So, if we use searchBar.backgroundColor = .green, we'll set the backgroundColor of Superview green. Therefore, the UISearchBar instance property backgroundColor will set the superview's background color.
Superview of UISearchBar
barTintColor for UISearchBarBackground
We can see the Class Name of red view is UISearchBarBackground in Object inspector.
However, there's no direct method to access the view, we can use KVC searchBar.value(forKey: "background") as? UIView try to get searchBarBackground.
If we use searchBar.barTintColor = .red, we'll set the backgroundColor of UISearchBarBackground's view red. In order to remove two black border on the tint bar layer, we have to remove the background from superview.
barTintColor of UISearchBar
searchField?.backgroundColor for UITextField
We can see the Class Name of yellow view is _UISearchBarSearchFieldBackgroundView (Subview of UISearchBarTextField) in Object inspector.
There's no direct method to access the searchField, same as searchBarBackground. We can also use KVC searchField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField try to get searchField.
If we use searchField?.backgroundColor = .yellow, we'll set the backgroundColor of UITextField yellow. Therefore, if we want to set text field background color, we have to access the searchField with KVC first
UITextField of UISearchBar
It's much simpler than that in Swift 5.
searchBar.barTintColor = .black
searchBar.searchTextField.backgroundColor = .white
Swift 4-5
searchController.searchBar.barTintColor = .white

How To Change UITextView Cursor Blink Color in Xcode

Hello people can anyone help us in how to change the cursor blink color of UITextView in Xcode using Swift language or from storyboard.
Swift 4, Xcode 9.1 Beta
I tried setting the tintColor of my UITextView in the interface builder but that didn't work for me. I don't know why, but setting the tintColor on my UITextView to one color, then resetting it to the color I really wanted worked for me...
myUITextView.tintColor = .anySystemColor
myUITextView.tintColor = .theColorYouReallyWant
For some reason that I don't know, I had to run a didChange method because tintColor on its own doesn't work.
BTW, I'm on Swift 5.
textView.tintColor = .white
textView.tintColorDidChange()
It works for me!
just change Tint Color
UITextView.appearance().tintColor = .black
Objective-C:
[[UITextView appearance] setTintColor:[UIColor blackColor]];
To do it in storyboard, do this
Set the Tint to your desired color.
I tried setting the tintColor of textView and textField in storyboard.
TextField cursor changed color, but textView didn't.
It helped me next:
let redTintColor: UIColor = UIColor.red
override func awakeFromNib() {
super.awakeFromNib()
textView.tintColor = redTintColor
}
or
let redTintColor: UIColor = UIColor.red
override func awakeFromNib() {
super.awakeFromNib()
textView.tintColor = redTintColor
textView.tintColorDidChange()
}
Just assign the tint color twice.
textView.tintColor = UIColor.black
textView.tintColor = UIColor.white
This works fine and looks better then double assign
DispatchQueue.main.async {
self.addressTextView.tintColor = UIColor.yourColor
}

How to place the TextField's divider under the LeftView as well by using Cosmicmind/Material TextField

I am using TextField by using Material Library. That's what the default implementation(Divider is under the Text Area only).
Is there any way to put the divider under the LeftView/Image and decrease the gap between Image and Text (like this).
Any help will be appreciated.
this could be the solution for your problem.....
let leftView = UIImageView()
leftView.image = Icon.phone?.tint(with: Color.blue.base)
textField.leftView = leftView
textField.leftViewMode = .always
When the leftView is laid out it sets the divider's edge inset equal to it's (the leftView) width. So you're going to need to change that. I am subclassing TextField, so I just overrode layoutSubviews.
override func layoutSubviews() {
super.layoutSubviews()
dividerContentEdgeInsets.left = 0
}

UISearchBar custom corners

I'm trying to create a search bar like this:
But I'm noticing that I'm probably going to have to replace the search bar with my own image because the search bar corners comes out wrong when I set:
self.searchController.searchBar.layer.cornerRadius = 50 // I've tried other numbers besides this too with no luck
self.searchController.searchBar.clipsToBounds = true
If I set this:
self.searchController.searchBar.layer.cornerRadius = self.searchController.searchBar.bounds.height/2
The search bar comes out like this:
Which still isn't exact like in the image.
Is there a way to replace the left and right side of the textfield with an image that way I can use the rounded corners from my custom search bar?
I am using this code UISearchBar but you can use this code with UISearchController.
let searchBar = UISearchBar()
searchBar.sizeToFit()
searchBar.placeholder = "Search"
navigationItem.titleView = searchBar
if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
// Rounded corner
backgroundview.layer.cornerRadius = 14;
backgroundview.clipsToBounds = true;
}
}
You should change the radius of searchTextField inside UISearchBar .
you can do that like this :
searchBar.searchTextField.layer.cornerRadius = 20
searchBar.searchTextField.layer.masksToBounds = true
* searchBar is an outlet of UISearchBar from storyBoard
The issue here is you are setting the corner radius on the UISearchBar, not the UITextField inside it. You can do some sort of hack to get the UITextField, but that's not really recommended.
As you mentioned in your question, you'll need to use custom images and the methods shown here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/#//apple_ref/doc/uid/TP40007529-CH3-SW40
This IS working for me in swift 3 iOS 10:
searchController.searchBar.layer.cornerRadius = 20
searchController.searchBar.clipsToBounds = true
ez way for searchbarview
for subview & POPUPs [Swift 5]
override func layoutSublayers(of layer: CALayer) {
searchBarPopup.clipsToBounds = true
searchBarPopup.layer.cornerRadius = 10
searchBarPopup.layer.maskedCorners = [ .layerMaxXMinYCorner, .layerMinXMinYCorner]
}

How to remove the outer border of UITextField presented in UIAlertActionController

I am adding multiple UITextFields to my alert controller, and I don't want that ugly black box around them. I know it's not the border property of the textField, because I have tried setting that and that influences the actual textField, not the box.
I have tried
textField.superview?.backgroundColor = UIColor.redColor()
textField.superview?.layer.borderColor = UIColor.redColor().CGColor
Setting the background Color works as expected, filling in the space between the textfield and the "black border" but setting the borderColor or borderWidth on superview.layer do nothing. Any ideas?
I ended up subclassing UIAlertController.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
for field in textFields! as [UITextField] {
field.superview?.superview?.layer.borderWidth = 2
field.superview?.superview?.layer.borderColor = UIColor.whiteColor().CGColor
}
}

Resources