Material design Outline textfield with white background - ios

Im trying to use material components library and make a textfield with outline and background color, but this is what I get
The white is outside the bounds.
Can you help me?
this is my code
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = UIImage(named: "user")
txtfieldEmail.placeholderLabel.textColor = UIColor.gray
txtfieldEmail.leftView = imageView
txtfieldEmail.leftViewMode = .always
//txtfieldEmail.backgroundColor = UIColor.white
//txtfieldEmail.translatesAutoresizingMaskIntoConstraints = false
//txtfieldEmail.clipsToBounds = true
txtfieldEmail.layer.backgroundColor = UIColor.white.cgColor
usernameTextFieldController = MDCTextInputControllerOutlined(textInput: txtfieldEmail)

After I kept editing the form and adding more content, I ended up finding that we should be defining the style in the MDCTextInputControllerOutlined
So all I had to do was usernameTextFieldController?.borderFillColor = UIColor.white

Related

UIButton UIEdgeInsets doesn't work

So I've looked at all projects on Stack Overflow for adding edge insets for UIImages in a UIButton but none of them worked. When I build the project, the image is just set through the bounds of the button - it's not set to the insets and I don't know why. Here is the code:
let Settingsbutton = AnimatableButton(frame: CGRect(x: 23, y: 30, width: 40, height: 40))
Settingsbutton.layer.cornerRadius = Settingsbutton.frame.size.height / 2
Settingsbutton.layer.backgroundColor = UIColor.white
let roundedButton3 = UIButton(type: .custom)
roundedButton3.frame = Settingsbutton.frame
let _border3 = CAShapeLayer()
_border3.path = UIBezierPath(roundedRect: roundedButton3.bounds, cornerRadius:roundedButton3.frame.size.width/2).cgPath
_border3.frame = roundedButton3.bounds
_border3.strokeColor = UIColor.white.cgColor
_border3.fillColor = UIColor.clear.cgColor
_border3.lineWidth = 3.0
Settingsbutton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
Settingsbutton.contentVerticalAlignment = .fill
Settingsbutton.contentHorizontalAlignment = .fill
Settingsbutton.layer.addSublayer(_border3)
self.view.addSubview(Settingsbutton)
self.view.bringSubview(toFront: Settingsbutton)
if PFUser.current() == nil{
Settingsbutton.setBackgroundImage(UIImage(named: "padlock"), for: .normal)
}else{
}
imageEdgeInsets are for positioning the image NOT the background image!
Use setImage instead of setBackgroundImage
If AnimatableButton is a UIButton why you want to add button inside button? I don't know really what's your purpose but you might try
Settingsbutton.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)

UITextField not getting displayed in View

I'm programatically creating a UITextField, called nameTextField in my viewDidLoad something like this:
var nameTextField = UITextField()
nameTextField = UITextField(frame: CGRect(x: 10, y: 20, width: 30, height: 20))
nameTextField.borderStyle = .roundedRect
nameTextField.autocorrectionType = UITextAutocorrectionType.no
nameTextField.returnKeyType = UIReturnKeyType.done
nameTextField.keyboardType = UIKeyboardType.default
nameTextField.backgroundColor = UIColor.white
nameTextField.placeholder = "Enter Your Full Name"
nameTextField.delegate = self
self.view.addSubview(nameTextField)
I'm not seeing this my view, what mistake Am I making? Thanks in advance.
may be your view background color is white so that reason it wont show change the background color
nameTextField.backgroundColor = UIColor.red
choice 2
if your VC is embed with navigation controller , change the Y frame and check
nameTextField = UITextField(frame: CGRect(x: 10, y: 90, width: 30, height: 20))
as well as change the bg color
nameTextField.backgroundColor = UIColor.red
You need to specify where you want the text field to show up relative to the main view of your view controller. For instance this would work: nameTextField.center = self.view.center

how to add a background image to a UITextView

I am trying to add a background image to my UITextView, but there doesn't seem to be an option to do so. I have tried adding an imageView with the image as a sub view
self.bioTextView.delegate = self
let imgView = UIImageView(frame: self.bioTextView.frame)
imgView.image = #imageLiteral(resourceName: "text_field_bio")
self.bioTextView.addSubview(imgView)
But this didn't work either. The color of the textView Background is clear. I have also tried including sending that imgView to the back, but that didn't work either.
add the foll0wing two lines and check once
bioTextView.addSubview(imgView)
bioTextView.sendSubview(toBack: imgView)
or use
bioTextView.backgroundColor = UIColor(patternImage: UIImage(named: "Image.png"))
updated
let textView = UITextView(frame: CGRect(x: CGFloat(20), y: CGFloat(20), width: CGFloat(400), height: CGFloat(400 )))
textView.text = "this is a test \n this is test \n this is a test"
let img = UIImageView(frame: textView.bounds)
img.image = UIImage(named: "1.jpg")
textView.backgroundColor = UIColor.clear
textView.addSubview( img)
self.view.addSubview(textView)
textView.sendSubview(toBack: img)
you get the output

Custom navigation bar image and text

I need in my app a custom navigation bar with an image and a text but I can't add the text.
Here is the code to add the image, how can I add the title?
let logo = #imageLiteral(resourceName: "navigationbaricon")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
Thanks
Where is the frame assigned for self.navigationItem.titleView? Set the frame for imageView and it will work.
You can wrap the UIImageView and the UILabel (which will hold the custom title) in an UIView and then assign the UIView to the self.navigationItem.titleView. Something like this:
let view = UIView(...);
let label = UILabel(...);
label.text = "Custom Title";
let image = UIImageView(image: UIImage(named: "..."));
view.addSubview(image);
view.addSubview(label);
self.navigationItem.titleView = view;
This one is worked for me
override func viewDidLoad() {
super.viewDidLoad()
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
titleView.backgroundColor = UIColor.redColor()
let imageView = UIImageView(image: UIImage(named: "img")!)
imageView.frame = CGRectMake(0, 0, 40, 40)
imageView.contentMode = .ScaleAspectFill
titleView.addSubview(imageView)
self.navigationItem.titleView = titleView
}

Swift how to make for navigation middle section image and text next to each other

I have searched a lot on stackoverflow and I havent been able to find solution for my problem which is that I need to put in navigationBar in middle section image and text next to each other but apparently I was able only to place just image or just text
Here is my code
let nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Default
nav?.tintColor = UIColor.blackColor()
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "icon_heart")
imageView.image = image
self.navigationItem.title = "App name"
self.navigationItem.titleView = imageView
I am aware that it on this way it accept only image which is titleView but I cant think of some normal solution and dont know is it even possible. Also if that is not possible how can I make then blank image and draw that icon image and text next to each other and append then that new image.
Edit:
Based on awph answer I have added code like this and it works fine for my case where I have always same characters in title
let screenSize: CGRect = UIScreen.mainScreen().bounds
let nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Default
nav?.tintColor = UIColor.blackColor()
let imageView = UIImageView(frame: CGRect(x: screenSize.width / 2 - 40, y: 15, width: 20, height: 20))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "icon_heart")
imageView.image = image
nav!.addSubview(imageView)
// label on top of the application that contain word
var label = UILabel(frame: CGRectMake(0, 0, 0, 0))
label.center = CGPointMake(screenSize.width / 2 - 15, 17)
label.textColor = UIColor.blackColor()
label.font = UIFont.systemFontOfSize(14)
label.textAlignment = NSTextAlignment.Center
label.text = "App Name"
label.sizeToFit()
nav!.addSubview(label)
You right, as said in documentation:
// Custom view to use in lieu of a title. May be sized horizontally. Only used when item is topmost on the stack.
What you need to do is to create an UIView, add an UImageView (that contains your image) as subview (view.addSubview(imageView)) and then add a UILabel on it view.addSubview(label).

Resources