I am using below code for adding left view in textfield:
func setUI()
{
txtUserCode.leftViewMode = .always
let label = UILabel(frame: CGRect(x: 7, y: 0, width: 18, height: txtUserCode.frame.size.height))
label.text = "#"
txtUserCode.leftView?.frame = CGRect(x: 0, y: 0, width: 18, height: txtUserCode.frame.size.height)
txtUserCode.leftView = label
label.backgroundColor = UIColor.green
txtUserCode.leftView?.backgroundColor = UIColor.red
}
But the problem is there is gap between left view and curser you can see in screen shot.
See there is small gap between curser and the left view, you can see between 'premB' and '#'
Any help or suggestion will be helpful.
Update your code to below.
func setUI()
{
txtUserCode.leftViewMode = .always
let label = UILabel(frame: CGRect(x: 7, y: 0, width: 18, height: txtUserCode.frame.size.height))
label.text = "#"
label.sizeToFit()
label.frame = CGRect(x: 7, y: 0, width: label.frame.size.width, height: txtUserCode.frame.size.height)
txtUserCode.leftView?.frame = CGRect(x: 0, y: 0, width: label.frame.size.width, height: txtUserCode.frame.size.height)
txtUserCode.leftView = label
label.backgroundColor = UIColor.green
txtUserCode.leftView?.backgroundColor = UIColor.red
}
To get the exact size, first make sizeToFit for the label & instead of 18, give the width of the label.
Add below line and try again.
txtUserCode.borderStyle = .none
Related
I have a text field where i have added a text and icon. Like screenshot below:
However i want to have space between the icon and the text..
I tried this code:
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
let image = UIImage(named: "username")
imageView.image = image
let viewLeft: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 20))
viewLeft.addSubview(imageView)
but got this:
how to add the space after the icon not before?
You can try below code :-
public func setLeftView(of image: UIImage!) {
//setting left image
self.paddingLeft = 50
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let paddingImage = UIImageView()
paddingImage.image = image
paddingImage.contentMode = .scaleAspectFit
paddingImage.frame = CGRect(x: 15, y: 0, width: 23, height: 40)
paddingView.addSubview(paddingImage)
self.leftView = paddingView
self.leftViewMode = UITextFieldViewMode.always
}
I'd like to customize my navigation bar and insert a UIView with two subviews into the title view of the navigation bar.
Unfortunately the subviews aren't displayed.
Do you have any idea why?
let titleContainer = UIView()
titleContainer.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
let searchIcon = UIButton()
searchIcon.setImage(UIImage(named: "search_icon"), for: UIControlState.normal)
searchIcon.layer.frame = CGRect(x: 0, y: (UIScreen.main.bounds.size.height - 28) / 2, width: 28, height: 28)
titleContainer.addSubview(searchIcon)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 28, y: UIScreen.main.bounds.size.height, width: UIScreen.main.bounds.size.width-28, height: UIScreen.main.bounds.size.height)
titleLabel.textColor = UIColor(red:255, green:255, blue:255, alpha:1.0)
titleContainer.addSubview(self.titleLabel)
self.navigationItem.titleView = titleContainer
problem and expected solution:
Why did you use height of all the element as UIScreen.main.bounds.size.height? Please try the below code.
P.S. I have changed the color of searchIcon as i didn't have the image and changed the color of text of label and added some text so that it is visible.
let titleContainer = UIView()
titleContainer.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 45)
let searchIcon = UIButton()
//searchIcon.setImage(UIImage(named: "search_icon"), for: UIControlState.normal)
searchIcon.backgroundColor = UIColor.red
searchIcon.layer.frame = CGRect(x: 0, y: 8, width: 28, height: 28)
titleContainer.addSubview(searchIcon)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 28, y: 8, width: UIScreen.main.bounds.size.width-28, height: 30)
titleLabel.textColor = UIColor.blue//(red:255, green:255, blue:255, alpha:1.0)
titleContainer.addSubview(titleLabel)
titleLabel.text = "Sample Text"
self.navigationItem.titleView = titleContainer
Here is the screenshot of the above code:
Please change the x,y accordingly as per your requirement.
Have you considered making your let bar = UINavigationBar() programmatically and then add it with: self.view.addSubview(bar)?
So you can push all the "UIBarItems" you want..?
I am trying to create the following registration form . I cant set the space between the textfield image and the placeholder text . I've tried the following :
Email_text.placeholderRect(forBounds: CGRect(x: 10, y: 0 , width: 60, height: 50))
any idea how can I fix this ?
Thanks
You can simply do like this, an extension for UITextField
extension UITextField {
func setTextIconAndPlaceholder(icon: UIImage, placeholder: String) {
let imageView = UIImageView()
imageView.image = icon
imageView.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
let view = UIView()
view.frame = CGRect(x: 0, y: 2, width: 25, height: 25)
view.backgroundColor = UIColor.clear
view.addSubview(imageView)
self.leftView = view
self.leftViewMode = UITextFieldViewMode.always
self.placeholder = placeholder
}
}
How do I add Text and Image on right of the UITextField.
I have a UITextField which will initially have a text/label at right of it and when user click on the button the same textField will now have text + image to the right of UITextField
Image can be added like this but what about the text/label and both ?
txtField.rightViewMode = .always
txtField.rightView = UIImageView(image: UIImage(named: "selectdrop"))
For that you can create on UIView instance and add the UILabel and UIImageView inside it then finally set that view as rightView of textField. Some thing like below.
let rightView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 40))
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 60, height: 40))
label.text = "Text"
label.textColor = UIColor.red
label.textAlignment = .center
let imageView = UIImageView(frame: CGRect(x: 40, y: 0, width: 40, height: 40))
imageView.image = UIImage(named: "selectdrop")
rightView.addSubview(label)
rightView.addSubview(imageView)
txtField.rightView = rightView
txtField.rightViewMode = .always
I want to calculate view size with autolayout, but it can not get right value. Here is my code
// build views
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let posterView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 100))
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
titleLabel.text = "Title "
titleLabel.font = UIFont.systemFontOfSize(16) // label height = 19.5
view.addSubview(posterView)
view.addSubview(titleLabel)
posterView.snp_makeConstraints { (make) in
make.top.left.right.equalTo(view).offset(0)
make.width.equalTo(posterView.snp_height).multipliedBy(16.0 / 9.0)
}
titleLabel.snp_makeConstraints { (make) in
make.top.equalTo(posterView.snp_bottom).offset(0)
make.left.equalTo(view).offset(0).priority(750)
make.right.equalTo(view).offset(0).priority(750)
make.bottom.equalTo(view.snp_bottom).offset(0)
}
// calculate
view.translatesAutoresizingMaskIntoConstraints = false
let heightConstraint = NSLayoutConstraint(item: view, height: 90+19.5)
view.addConstraint(heightConstraint)
let fittingSize = view.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
// the fittingSize is (40,109.5), not expect value (160, 109.5)
Please don't tell me how to calculate dynamic height for UITableViewCell. This is a opposite problem from height to width.
Thanks