Swift - UITextField Programmatically Cannot enter text - ios

I'm new to Swift programming, I'm trying to build an app with UITextField in it, But I'm having a problem, I created an UITextField programmatically but it seems like I cannot enter a text in it. Here's my viewDidLoad
Code:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView = UITableView(frame: self.tableView.frame, style: .Grouped)
self.groupInfoView.frame = CGRectMake(0, 44, self.view.frame.width, 100)
self.groupInfoView.backgroundColor = UIColor.orangeColor()
self.groupInfoView.userInteractionEnabled = true
groupImg.frame = CGRectMake(10, 50, 40, 40)
groupImg.layer.cornerRadius = 5
groupImg.layer.masksToBounds = true
groupImg.image = UIImage(named: "group-def-img")
groupTitle = UITextField.init(frame:CGRectMake(groupImg.frame.maxX + 5, 50, self.groupInfoView.frame.width-65, 40))
groupTitle.backgroundColor = UIColor.whiteColor()
groupTitle.layer.cornerRadius = 5
groupTitle.layer.masksToBounds = true
groupTitle.placeholder = "Enter Group Title"
groupTitle.textColor = UIColor.orangeColor()
groupTitle.textAlignment = .Center
groupTitle.font = UIFont(name: "HelveticaNeue-Light", size: 18)
groupTitle.autocorrectionType = UITextAutocorrectionType.No
groupTitle.keyboardType = UIKeyboardType.Default
groupTitle.returnKeyType = UIReturnKeyType.Done
groupTitle.clearButtonMode = UITextFieldViewMode.WhileEditing;
groupTitle.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
groupTitle.enabled = true
groupTitle.userInteractionEnabled = true
groupTitle.delegate = self
groupTitle.text = "asda"
recipientLabel.frame = CGRectMake(5, 0, self.view.frame.width - 20, 40)
recipientLabel.font = UIFont(name: "HelveticaNeue-Light", size: 15)
recipientLabel.numberOfLines = 3
recipientLabel.textColor = UIColor.orangeColor()
recipientLabel.backgroundColor = UIColor.whiteColor()
recipientLabel.center.x = self.view.center.x
recipientLabel.layer.cornerRadius = 7
recipientLabel.layer.masksToBounds = true
recipientLabel.text = " To:"
self.groupInfoView.addSubview(groupTitle)
self.groupInfoView.addSubview(recipientLabel)
self.groupInfoView.addSubview(groupImg)
searchBar.delegate = self
self.titleLabel.text = "New Group Message"
navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.tabBarController?.tabBar.barStyle = .Black
self.navigationController!.navigationBar.translucent = false
self.navigationController!.navigationBar.barTintColor = UIColor.orangeColor()
tableView.separatorInset = UIEdgeInsets(top: 0, left: 65, bottom: 0, right: 0)
tableView.registerClass(UserCell.self, forCellReuseIdentifier: cellId)
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(handleCancel))
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .Done, target: self, action: #selector(handleDone))
fetchUser()
self.tableView.allowsMultipleSelection = true
if let navigationBar = self.navigationController?.navigationBar {
let firstFrame = CGRectMake(0, -5, (self.navigationController?.navigationBar.frame.width)!, 40)
let secondFrame = CGRectMake(0, 17, (self.navigationController?.navigationBar.frame.width)!, 30)
titleLabel.frame = firstFrame
titleLabel.textColor = UIColor.whiteColor()
titleLabel.font = UIFont(name: "HelveticaNeue-SemiBold", size: 18)
titleLabel.textAlignment = .Center
countLabel.textColor = UIColor.whiteColor()
countLabel.font = UIFont(name: "HelveticaNeue-Light", size: 13)
countLabel.frame = secondFrame
countLabel.textAlignment = .Center
navigationBar.addSubview(groupInfoView)
navigationBar.addSubview(titleLabel)
navigationBar.addSubview(countLabel)
}
And this is my references:
Code:
var titleLabel = UILabel()
var countLabel = UILabel()
var recipientLabel = UILabel()
var groupImg = UIImageView()
var groupTitle: UITextField = UITextField()
var groupInfoView = UIView()

Declare its globally so you can access it anywhere. and declare its delegates UITextFieldDelegate Headerimageview is my heartimage and HeaderView is my simple header view.
#IBOutlet var Headerimageview: UIImageView!
#IBOutlet var HeaderView: UIView!
var groupTitle : UITextField = UITextField()
In viewDidLoad()
groupTitle = UITextField.init(frame: CGRectMake(Headerimageview.frame.size.width + 25, 5, self.view.frame.width-85, 40))
groupTitle.backgroundColor = UIColor.whiteColor()
groupTitle.layer.cornerRadius = 5
groupTitle.layer.borderWidth = 1.0
groupTitle.layer.borderColor = UIColor.brownColor().CGColor
groupTitle.layer.masksToBounds = true
groupTitle.placeholder = "Enter Group Title"
groupTitle.textColor = UIColor.orangeColor()
groupTitle.textAlignment = .Center
groupTitle.font = UIFont(name: "HelveticaNeue-Light", size: 18)
groupTitle.autocorrectionType = UITextAutocorrectionType.No
groupTitle.keyboardType = UIKeyboardType.Default
groupTitle.returnKeyType = UIReturnKeyType.Done
groupTitle.clearButtonMode = UITextFieldViewMode.WhileEditing
groupTitle.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
groupTitle.enabled = true
groupTitle.userInteractionEnabled = true
groupTitle.delegate = self
self.HeaderView.addSubview(groupTitle)
Output :
TextField should begin editing method called
TextField did begin editing method called
While entering the characters this method gets called
While entering the characters this method gets called
While entering the characters this method gets called
TextField should return method called
TextField should snd editing method called
TextField did end editing method called
Update your this code with mine one...
groupTitle = UITextField.init(frame:CGRectMake(groupImg.frame.maxX + 5, 25, self.groupInfoView.frame.width-65, 40))
groupTitle = UITextField.init(frame: CGRectMake(groupImg.frame.size.width + 25, 25, self.view.frame.width-85, 40))
self.groupInfoView.addSubview(groupTitle)
self.groupInfoView.addSubview(groupImg)
self.view.addSubview(groupInfoView)

Related

The buttons will not show up in the IOS simulator, can anyone assist? (Code Provided Below)

The code below shows a potential login screen and everything works except for the buttons. I am trying to figure out why that is......
I thought maybe it was the version of Xcode i was using(Xcode 13), but the still doesn't seem like the reason or i would have received an error message.
import UIKit
class LoginViewController: UIViewController, UITextFieldDelegate{
struct Constants {
static let cornerRadius: CGFloat = 8.0
}
//Declaration of login fields
override func viewDidLoad() {
super.viewDidLoad()
loginButton.addTarget(self, action: #selector(didTaploginButton), for: .touchUpInside)
createAccountButton.addTarget(self, action: #selector(didTapcreateAccountButton), for: .touchUpInside)
termsButton.addTarget(self, action: #selector(didTaptermsButton), for: .touchUpInside)
privacyButton.addTarget(self, action: #selector(didTapprivacyButton), for: .touchUpInside)
usernameEmailField.delegate = self
passwordField.delegate = self
addSubviews()
view.backgroundColor = .systemBackground
// Do any additional setup after loading the view.
}
private let usernameEmailField: UITextField = {
let field = UITextField()
field.placeholder = "Username or Email"
field.returnKeyType = .next
field.leftViewMode = .always
field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
field.autocapitalizationType = .none
field.autocorrectionType = .no
field.layer.masksToBounds = true
field.layer.cornerRadius = Constants.cornerRadius
field.backgroundColor = .secondarySystemBackground
return field
}()
private let passwordField: UITextField = {
let field = UITextField()
field.placeholder = "Password"
field.returnKeyType = .next
field.leftViewMode = .always
field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
field.autocapitalizationType = .none
field.autocorrectionType = .no
field.layer.masksToBounds = true
field.layer.cornerRadius = Constants.cornerRadius
field.backgroundColor = .secondarySystemBackground
return field
}()
private let loginButton: UIButton = {
let loginbutton = UIButton()
loginbutton.setTitle("Log In", for: .normal)
loginbutton.layer.masksToBounds = true
loginbutton.layer.cornerRadius = Constants.cornerRadius
loginbutton.backgroundColor = .link
loginbutton.setTitleColor(.white, for: .normal)
return UIButton()
}()
private let termsButton: UIButton = {
let button = UIButton()
button.setTitle("Terms of Service", for: .normal)
button.setTitleColor(.secondaryLabel, for: .normal)
return UIButton()
}()
private let privacyButton: UIButton = {
return UIButton()
}()
private let createAccountButton: UIButton = {
let button = UIButton()
button.setTitleColor(.lightGray, for: .normal)
button.setTitle("New User? Create an Account", for: .normal)
button.backgroundColor = .systemBlue
button.layer.cornerRadius = Constants.cornerRadius
button.setTitleColor(.label, for: .normal)
return UIButton()
}()
//Picture behind the Logo
private let headerView: UIView = {
let header = UIView()
header.clipsToBounds = true
let backgroundImageView = UIImageView(image: UIImage(named: "Bluewater"))
header.addSubview(backgroundImageView)
return header
}()
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//assigning frames
headerView.frame = CGRect(x: 0,
y: 0.0,
width: view.width,
height: view.height/3.0
)
usernameEmailField.frame = CGRect(x: 25,
y: headerView.bottom + 10,
width: view.width-50,
height: 52.0
)
passwordField.frame = CGRect(x: 25,
y: usernameEmailField.bottom + 10,
width: view.width-50,
height: 52.0
)
loginButton.frame = CGRect(x: 25,
y: passwordField.bottom + 10,
width: view.width-50,
height: 52.0
)
createAccountButton.frame = CGRect(x: 25,
y: loginButton.bottom + 10,
width: view.width-50,
height: 52.0
)
termsButton.frame = CGRect(x: 10, y: view.height-view.safeAreaInsets.bottom-100, width: view.width-20, height: 50)
configurationView()
}
private func configurationView() {
guard headerView.subviews.count == 1 else {
return
}
guard let backgroundView = headerView.subviews.first else {
return
}
backgroundView.frame = headerView.bounds
//Pic Pool Logo
let imageView = UIImageView(image: UIImage(named: "ClearLogo"))
headerView.addSubview(imageView)
imageView.contentMode = .scaleAspectFit
imageView.frame = CGRect(x: headerView.width/4.0,
y: view.safeAreaInsets.top,
width: headerView.width/2.0,
height: headerView.height - view.safeAreaInsets.top)
}
private func addSubviews(){
view.addSubview(usernameEmailField)
view.addSubview(passwordField)
view.addSubview(loginButton)
view.addSubview(termsButton)
view.addSubview(privacyButton)
view.addSubview(createAccountButton)
view.addSubview(headerView)
}
#objc private func didTaploginButton() {}
#objc private func didTaptermsButton() {}
#objc private func didTapprivacyButton() {}
#objc private func didTapcreateAccountButton() {}
}
Take a real hard look at...
private let loginButton: UIButton = {
let loginbutton = UIButton()
loginbutton.setTitle("Log In", for: .normal)
loginbutton.layer.masksToBounds = true
loginbutton.layer.cornerRadius = Constants.cornerRadius
loginbutton.backgroundColor = .link
loginbutton.setTitleColor(.white, for: .normal)
return UIButton()
}()
see anything wrong? Maybe focus in on return UIButton()
So, changing it to...
private let loginButton: UIButton = {
let loginbutton = UIButton()
loginbutton.setTitle("Log In", for: [])
loginbutton.layer.masksToBounds = true
loginbutton.layer.cornerRadius = Constants.cornerRadius
loginbutton.backgroundColor = .link
loginbutton.setTitleColor(.white, for: [])
return loginbutton
}()
will produce...
Runnable example
//
// ViewController.swift
// StackOverflow
//
// Created by Shane Whitehead on 4/5/2022.
//
import UIKit
class ViewController: UIViewController, UITextFieldDelegate{
struct Constants {
static let cornerRadius: CGFloat = 8.0
}
//Declaration of login fields
override func viewDidLoad() {
super.viewDidLoad()
loginButton.addTarget(self, action: #selector(didTaploginButton), for: .touchUpInside)
createAccountButton.addTarget(self, action: #selector(didTapcreateAccountButton), for: .touchUpInside)
termsButton.addTarget(self, action: #selector(didTaptermsButton), for: .touchUpInside)
privacyButton.addTarget(self, action: #selector(didTapprivacyButton), for: .touchUpInside)
usernameEmailField.delegate = self
passwordField.delegate = self
addSubviews()
view.backgroundColor = .systemBackground
// Do any additional setup after loading the view.
}
private let usernameEmailField: UITextField = {
let field = UITextField()
field.placeholder = "Username or Email"
field.returnKeyType = .next
field.leftViewMode = .always
field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
field.autocapitalizationType = .none
field.autocorrectionType = .no
field.layer.masksToBounds = true
field.layer.cornerRadius = Constants.cornerRadius
field.backgroundColor = .secondarySystemBackground
return field
}()
private let passwordField: UITextField = {
let field = UITextField()
field.placeholder = "Password"
field.returnKeyType = .next
field.leftViewMode = .always
field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
field.autocapitalizationType = .none
field.autocorrectionType = .no
field.layer.masksToBounds = true
field.layer.cornerRadius = Constants.cornerRadius
field.backgroundColor = .secondarySystemBackground
return field
}()
private let loginButton: UIButton = {
let loginbutton = UIButton()
loginbutton.setTitle("Log In", for: [])
loginbutton.layer.masksToBounds = true
loginbutton.layer.cornerRadius = Constants.cornerRadius
loginbutton.backgroundColor = .link
loginbutton.setTitleColor(.white, for: [])
return loginbutton
}()
private let termsButton: UIButton = {
let button = UIButton()
button.setTitle("Terms of Service", for: [])
button.setTitleColor(.secondaryLabel, for: [])
return button
}()
private let privacyButton: UIButton = {
return UIButton()
}()
private let createAccountButton: UIButton = {
let button = UIButton()
button.setTitleColor(.lightGray, for: [])
button.setTitle("New User? Create an Account", for: [])
button.backgroundColor = .systemBlue
button.layer.cornerRadius = Constants.cornerRadius
button.setTitleColor(.label, for: .normal)
return button
}()
//Picture behind the Logo
private let headerView: UIView = {
let header = UIView()
header.clipsToBounds = true
let backgroundImageView = UIImageView(image: UIImage(named: "Bluewater"))
header.addSubview(backgroundImageView)
return header
}()
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//assigning frames
headerView.frame = CGRect(x: 0,
y: 0.0,
width: view.width,
height: view.height/3.0
)
usernameEmailField.frame = CGRect(x: 25,
y: headerView.bottom + 10,
width: view.width-50,
height: 52.0
)
passwordField.frame = CGRect(x: 25,
y: usernameEmailField.bottom + 10,
width: view.width-50,
height: 52.0
)
loginButton.frame = CGRect(x: 25,
y: passwordField.bottom + 10,
width: view.width-50,
height: 52.0
)
createAccountButton.frame = CGRect(x: 25,
y: loginButton.bottom + 10,
width: view.width-50,
height: 52.0
)
termsButton.frame = CGRect(x: 10, y: view.height-view.safeAreaInsets.bottom-100, width: view.width-20, height: 50)
configurationView()
}
private func configurationView() {
guard headerView.subviews.count == 1 else {
return
}
guard let backgroundView = headerView.subviews.first else {
return
}
backgroundView.frame = headerView.bounds
//Pic Pool Logo
let imageView = UIImageView(image: UIImage(named: "ClearLogo"))
headerView.addSubview(imageView)
imageView.contentMode = .scaleAspectFit
imageView.frame = CGRect(x: headerView.width/4.0,
y: view.safeAreaInsets.top,
width: headerView.width/2.0,
height: headerView.height - view.safeAreaInsets.top)
}
private func addSubviews(){
view.addSubview(usernameEmailField)
view.addSubview(passwordField)
view.addSubview(loginButton)
view.addSubview(termsButton)
view.addSubview(privacyButton)
view.addSubview(createAccountButton)
view.addSubview(headerView)
}
#objc private func didTaploginButton() {}
#objc private func didTaptermsButton() {}
#objc private func didTapprivacyButton() {}
#objc private func didTapcreateAccountButton() {}
}
extension UIView {
var width: CGFloat {
return bounds.width
}
var height: CGFloat {
return bounds.height
}
var bottom: CGFloat {
return frame.minY + bounds.height
}
}
I'd also, strongly, recommend either making the time to learn how to use auto layout OR SwiftUI instead

how to send a text with a send button to text field on swift iOS

I really new. all I want to do is send in the text I got on the password field as a secure text to the text field if that's possible. kind of like sending a text but with the secure text. the secure button just lets you see what is inside. I got that to work but I don't know how to transfer the information from one place to the other. do I create a function? Im sorry im a complete noob.
import UIKit
class ViewController: UIViewController {
private let scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.clipsToBounds = true
return scrollView
}()
private let secureButton : UIButton = {
let button = UIButton()
button.setTitle("Secure", for: .normal)
button.backgroundColor = .link
button.setTitleColor(.white, for: .normal)
button.layer.cornerRadius = 12
button.titleLabel?.font = .systemFont(ofSize: 20, weight: .bold)
button.layer.masksToBounds = true
button.addTarget( self, action: #selector(securebuttonTapped), for: .touchUpInside)
return button
}()
let sendButton : UIButton = {
let button = UIButton()
button.setTitle("Send", for: .normal)
button.backgroundColor = .green
button.setTitleColor(.white, for: .normal)
button.layer.cornerRadius = 12
button.titleLabel?.font = .systemFont(ofSize: 20, weight: .bold)
button.layer.masksToBounds = true
button.addTarget( self, action: #selector(sendButtonTapped), for: .touchUpInside)
return button
}()
let textView = UITextView()
private let passwordField: UITextField = {
let field = UITextField()
field.autocapitalizationType = .none
field.autocorrectionType = .no
field.returnKeyType = .done
field.layer.cornerRadius = 12
field.layer.borderWidth = 1
field.layer.borderColor = UIColor.lightGray.cgColor
field.placeholder = "Password"
field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 0))
field.leftViewMode = .always
field.backgroundColor = .white
field.isSecureTextEntry = true
return field
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.backgroundColor = .gray
view.addSubview(scrollView)
scrollView.addSubview(passwordField)
scrollView.addSubview(secureButton)
scrollView.addSubview(textView)
scrollView.addSubview(sendButton)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.frame = view.bounds
passwordField.frame = CGRect(x: 30,
y: 100,
width: scrollView.width-60,
height: 52)
secureButton.frame = CGRect(x: 30,
y: passwordField.bottom+10 ,
width: scrollView.width-60,
height: 30)
textView.frame = CGRect(x: 30,
y: secureButton.bottom+10 ,
width: scrollView.width-60,
height: 30)
sendButton.frame = CGRect(x: 30,
y: textView.bottom+10 ,
width: scrollView.width-60,
height: 30)
}
#objc func securebuttonTapped() {
let text = passwordField
text.isSecureTextEntry = !text.isSecureTextEntry
}
#objc func sendButtonTapped() {
let text = passwordField
textView.inputView = text
print("send button tapped")
}
}
Swift 5
if you want to pass the data in the same controller its very simple. just asign the text to the textField on button Pressed
var textField = UITexField()
#objc func sendButtonTapped() {
// here you will assign the text you got from the password field to watever textfield you want, whether it is secure text or simple text.
textField.text = passwordField.text
print("send button tapped")
}
if you want to pass that text to some other ViewController you need to send like this
let vc = UIStoryboard(name: "Main", bundle:
nil).instantiateViewController(withIdentifier: "YourViewController") as!
YourViewController
// here you will assign the text to the variable from other viewcontroller you
want to.
vc.text = "Enter the text here"
self.navigationController?.pushViewController(vc, animated: true)

how to print text from textField in label programmatically swift

i like to print textField Value in label by clicking button key. but i have been unable to do it. below is codes
import UIKit
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.gray
setupLabel()
setupTextField()
setupButton()
}
func setupLabel() {
let label = UILabel()
label.frame = CGRect(x: 40, y: 80, width: 300, height: 60)
label.text = "welcome to my world"
label.textColor = UIColor.yellow
label.font = UIFont.boldSystemFont(ofSize: 25)
label.textAlignment = .center
label.layer.borderWidth = 2
label.layer.borderColor = UIColor.yellow.cgColor
label.layer.cornerRadius = 5
view.addSubview(label)
}
func setupTextField() {
let textField = UITextField()
textField.frame = CGRect(x: 10, y: 200, width: self.view.frame.size.width - 20, height: 60)
textField.placeholder = "text here"
textField.textAlignment = .center
textField.font = UIFont.systemFont(ofSize: 25)
textField.layer.borderWidth = 2
textField.layer.borderColor = UIColor.yellow.cgColor
textField.layer.cornerRadius = 5
view.addSubview(textField)
}
func setupButton() {
let button = UIButton()
button.frame = CGRect(x: 50, y: 300, width: self.view.frame.size.width - 100, height: 60)
button.setTitle("Enter", for: .normal)
button.setTitleColor(UIColor.yellow, for: .normal)
button.layer.borderWidth = 2
button.layer.borderColor = UIColor.yellow.cgColor
button.layer.cornerRadius = 5
button.addTarget(self, action: #selector(buttonTarget), for: .touchUpInside)
view.addSubview(button)
}
func buttonTarget() {
// i missed of here
}
}
in here you need to follow two steps
you need to create the label and textfield in globally not instance, for e.g
class MainViewController: UIViewController
let label = UILabel()
let textField = UITextField()
hide the instance vale of your allocation
func setupLabel() {
//let label = UILabel()
label.frame = CGRect(x: 40, y: 80, width: 300, height: 60)
label.text = "welcome to my world"
label.textColor = UIColor.yellow
label.font = UIFont.boldSystemFont(ofSize: 25)
label.textAlignment = .center
label.layer.borderWidth = 2
label.layer.borderColor = UIColor.yellow.cgColor
label.layer.cornerRadius = 5
view.addSubview(label)
}
then you can access the value directly anywhere in your class
func buttonTarget() {
if let text = textField.text
{
label.text = text
}
}

Swift Change Position of UIBarButtonItem in UINavigationBar

I am doing custom a navigation bar with items which y positions are lower than default. I tried many methods:
setBackButtonBackgroundVerticalPositionAdjustment:
set CGRectOffset of custom view
UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset.init(horizontal: X, vertical: Y), forBarMetrics: UIBarMetrics.Default)
They made the items appeared in right positions, but I couldn't fire any actions when I clicked/touched on them. I think because I didn't touch the bar items, they were only the appearances/ copy views/images of these item.
Could anybody help me to resolve this problem?
My code:
let lblTitle = UILabel(frame: CGRectMake(0, 0, 100, 28))
lblTitle.text = FMLStringLocale("APP_name")
lblTitle.textColor = UIColor.whiteColor()
lblTitle.font = ROBOTO_THIN_24
lblTitle.textAlignment = NSTextAlignment.Left
lblTitle.backgroundColor = UIColor.clearColor()
lblTitle.transform = CGAffineTransformMakeTranslation(6, 11)
let lblTitleContainer = UIView(frame: lblTitle.frame)
lblTitleContainer.addSubview(lblTitle)
let leftTitle = UIBarButtonItem(customView: lblTitleContainer)
navigationItem.leftBarButtonItem = leftTitle
let rightView = UIView(frame: CGRectMake(0, 0, 134, 22))
rightView.backgroundColor = UIColor.whiteColor()
rightView.transform = CGAffineTransformMakeTranslation(0, 18)
let imvUSFlag = UIImageView(image: UIImage(named: "icon_us"))
imvUSFlag.frame = CGRectMake(0, 0, 19, 13)
imvUSFlag.transform = CGAffineTransformMakeTranslation(-2, 20)
rightView.addSubview(imvUSFlag)
let switcher = UISwitch(frame: CGRectMake(imvUSFlag.frame.maxX - 5, 12, 31, 16))
switcher.setOn(false, animated: false)
switcher.addTarget(self, action: Selector("tapToChangeLanguage:"), forControlEvents: UIControlEvents.ValueChanged)
switcher.transform = CGAffineTransformMakeScale(0.5, 0.5)
// let switchContainer = UIView(frame: switcher.frame)
// switchContainer.addSubview(switcher)
rightView.addSubview(switcher)
let imvFRFlag = UIImageView(image: UIImage(named: "icon_france"))
imvFRFlag.frame = CGRectMake(switcher.frame.maxX + 6, 0, 19, 14)
imvFRFlag.transform = CGAffineTransformMakeTranslation(0, 20)
rightView.addSubview(imvFRFlag)
let btnMenu = UIButton()
btnMenu.setImage(UIImage(named : "icon_menu"), forState: .Normal)
btnMenu.frame = CGRectMake(imvFRFlag.frame.maxX + 30, 0, 23, 21)
btnMenu.addTarget(self, action: "tapToMenu:", forControlEvents: UIControlEvents.TouchUpInside)
btnMenu.transform = CGAffineTransformMakeTranslation(0, 18)
rightView.addSubview(btnMenu)
rightView.transform = CGAffineTransformMakeTranslation(0, 18)
let rightViewItem = UIBarButtonItem(customView: rightView)
navigationItem.rightBarButtonItem = rightViewItem
navigationController?.navigationBar.barStyle = .BlackTranslucent
navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
navigationController?.navigationBar.shadowImage = UIImage()
The reason why you can't tap your button btnMenu is that you have positioned btnMenu outside the frame of its superview rightView. A subview outside its superview cannot receive touches.
Finally, I resolved my problem:
Check my new code:
let lblTitle = UILabel(frame: CGRectMake(7, 32, 100, 28))
lblTitle.text = FMLStringLocale("APP_name")
lblTitle.textColor = UIColor.whiteColor()
lblTitle.font = ROBOTO_THIN_24
lblTitle.textAlignment = NSTextAlignment.Left
lblTitle.backgroundColor = UIColor.clearColor()
let lblTitleContainer = UIView(frame: CGRectMake(0, 0, 100, 64))
// lblTitleContainer.bounds = CGRectOffset(lblTitleContainer.bounds, -6, -32)
lblTitleContainer.addSubview(lblTitle)
let leftTitle = UIBarButtonItem(customView: lblTitleContainer)
navigationItem.leftBarButtonItem = leftTitle
let rightView = UIView(frame: CGRectMake(0, 0, 134, 64))
// rightView.backgroundColor = UIColor.grayColor()
// rightView.transform = CGAffineTransformMakeTranslation(0, 18)
let imvUSFlag = UIImageView(image: UIImage(named: "icon_us"))
imvUSFlag.frame = CGRectMake(0, 40, 18, 12)
rightView.addSubview(imvUSFlag)
let switcher = UISwitch(frame: CGRectMake(imvUSFlag.frame.maxX - 5, 32, 31, 16))
switcher.setOn(false, animated: false)
switcher.addTarget(self, action: Selector("tapToChangeLanguage:"), forControlEvents: UIControlEvents.ValueChanged)
switcher.transform = CGAffineTransformMakeScale(0.5, 0.45)
let switchContainer = UIView(frame: switcher.frame)
switchContainer.addSubview(switcher)
rightView.addSubview(switcher)
let imvFRFlag = UIImageView(image: UIImage(named: "icon_france"))
imvFRFlag.frame = CGRectMake(switcher.frame.maxX + 6, 40, 18, 12)
rightView.addSubview(imvFRFlag)
let btnMenu = UIButton()
btnMenu.setImage(UIImage(named : "icon_menu"), forState: .Normal)
btnMenu.frame = CGRectMake(imvFRFlag.frame.maxX + 30, 35, 23, 21)
btnMenu.addTarget(self, action: "tapToMenu:", forControlEvents: UIControlEvents.TouchUpInside)
rightView.addSubview(btnMenu)
let rightViewItem = UIBarButtonItem(customView: rightView)
navigationItem.rightBarButtonItem = rightViewItem
navigationController?.navigationBar.barStyle = .BlackTranslucent
navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
navigationController?.navigationBar.shadowImage = UIImage()

modal segue slow first time when triggered

I've created a modal segue to a viewController, however when i trigger this segue, there seem to be some delay when viewing the viewController. All i have in the viewController is in the viewDidLoad. what could course this slow modal segue?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Caption".uppercaseString
self.view.backgroundColor = UIColor(hexString: "#0B1E30")
self.navigationController?.navigationBar.barTintColor = UIColor(gradientStyle: UIGradientStyle.LeftToRight, withFrame: CGRectMake(0, 0, self.view.frame.width, (self.navigationController?.navigationBar.frame.height)! + 20), andColors: [UIColor(hexString: "#7F5CE6"), UIColor(hexString: "#9D8FE2")])
let closeButton = UIBarButtonItem(image: UIImage(named: "Cross"), style: UIBarButtonItemStyle.Done, target: self, action: "dismissController")
self.navigationItem.leftBarButtonItem = closeButton
textView?.text = "Add caption (optional)"
textView?.becomeFirstResponder()
textView?.keyboardAppearance = UIKeyboardAppearance.Dark
textView?.font = UIFont(name: "Montserrat-Light", size: 13)
textView?.textColor = UIColor(hexString: "#363636")
capturedImageView = UIImageView(image: capturedPhoto)
capturedImageView?.frame = CGRectMake(8,10, 85, 85)
let path = UIBezierPath(rect: CGRectMake(8, 10, 85, 85))
textView?.textContainer.exclusionPaths = [path]
textView?.addSubview(capturedImageView!)
facebookButton?.tag = 1
facebookButton!.adjustsImageWhenHighlighted = false
facebookButton?.addTarget(self, action: "tapSocial:", forControlEvents: UIControlEvents.TouchUpInside)
facebookButton?.tintColor = UIColor.whiteColor()
facebookButton?.backgroundColor = UIColor(hexString: "#395798")
facebookButton?.contentMode = UIViewContentMode.Center
facebookButton?.setTitle("", forState: UIControlState.Normal)
let fBorder = CALayer()
let width = CGFloat(1.0)
fBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
fBorder.frame = CGRect(x: 0, y: facebookButton!.frame.size.height - width, width: facebookButton!.frame.size.width, height: facebookButton!.frame.size.height)
fBorder.borderWidth = width
facebookButton!.layer.addSublayer(fBorder)
facebookButton!.layer.masksToBounds = true
twitterButton?.tag = 2
twitterButton!.adjustsImageWhenHighlighted = false
twitterButton?.addTarget(self, action: "tapSocial:", forControlEvents: UIControlEvents.TouchUpInside)
twitterButton?.contentMode = UIViewContentMode.Center
twitterButton?.tintColor = UIColor.whiteColor()
twitterButton?.setTitle("", forState: UIControlState.Normal)
let tBorder = CALayer()
tBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
tBorder.frame = CGRect(x: 0, y: twitterButton!.frame.size.height - width, width: twitterButton!.frame.size.width, height: twitterButton!.frame.size.height)
tBorder.borderWidth = width
twitterButton!.layer.addSublayer(tBorder)
twitterButton!.layer.masksToBounds = true
let tRightBorder = CALayer()
tRightBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
tRightBorder.frame = CGRect(x: 0, y: 0, width: width, height: twitterButton!.frame.size.height)
tRightBorder.borderWidth = width
twitterButton!.layer.addSublayer(tRightBorder)
instagramButton?.tag = 3
instagramButton!.adjustsImageWhenHighlighted = false
instagramButton?.addTarget(self, action: "tapSocial:", forControlEvents: UIControlEvents.TouchUpInside)
instagramButton?.contentMode = UIViewContentMode.Center
instagramButton?.tintColor = UIColor.whiteColor()
instagramButton?.setTitle("", forState: UIControlState.Normal)
let iBorder = CALayer()
iBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
iBorder.frame = CGRect(x: 0, y: instagramButton!.frame.size.height - width, width: instagramButton!.frame.size.width, height: instagramButton!.frame.size.height)
iBorder.borderWidth = width
instagramButton!.layer.addSublayer(iBorder)
instagramButton!.layer.masksToBounds = true
let iLeftBorder = CALayer()
iLeftBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
iLeftBorder.frame = CGRect(x: 0, y: 0, width: width, height: instagramButton!.frame.size.height)
iLeftBorder.borderWidth = width
instagramButton!.layer.addSublayer(iLeftBorder)
}
This came as a surprise for me but apparently there is a performance issue with textView's property "selectable" when set to true by default (checked on Storyboard).
Try going to your Storyboard, select each textView and uncheck the box "selectable". If you need the textView to be selectable, then just set selectable to true programatically on your viewDidLoad.

Resources