Hiding and showing (subviews) button or change title? - ios

I have a problem with my subviews. I have 2 visible buttons and 1 which should show after click the first one. More Precisely, i have Start Button, Reset Button and Stop Button. On load should show just Start and Reset button, but when I press Start Button the Reset button should hide and stop button should show. Syntax like isHidden doesn't works. What is the problem?
Star, Stop and Reset Button:
var stopButton: UIButton{
let stopButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100))
stopButton.backgroundColor = .white
stopButton.setTitle("Stop", for: .normal)
stopButton.addTarget(self, action: #selector(stopButtonAction), for: .touchUpInside)
stopButton.layer.cornerRadius = 50
stopButton.layer.masksToBounds = true
stopButton.isHidden = true
return stopButton
}
var resetButton: UIButton{
let resetButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100))
resetButton.backgroundColor = .red
resetButton.setTitle("Reset", for: .normal)
resetButton.addTarget(self, action: #selector(resetButtonAction), for: .touchUpInside)
resetButton.layer.cornerRadius = 50
resetButton.layer.masksToBounds = true
return resetButton
}
var startButton: UIButton{
let startButton = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
startButton.backgroundColor = .green
startButton.setTitle("Start", for: .normal)
startButton.addTarget(self, action: #selector(startButtonAction), for: .touchUpInside)
startButton.layer.cornerRadius = 50
startButton.layer.masksToBounds = true
return startButton
}
Here i add this Subviews(this func also add UIView and it's called in viewDidLoad):
func addBottomView()
{
let orginX = (collectionView?.frame.minX)!
let orginY = (collectionView?.frame.maxY)!
let heightOfView = view.frame.height - (view.frame.height / 100) * 70
let bottomView = UIView(frame: CGRect(x: orginX, y: orginY, width: view.frame.width, height: heightOfView))
bottomView.backgroundColor = .orange
view.addSubview(bottomView)
bottomView.addSubview(stopButton)
bottomView.addSubview(startButton)
bottomView.addSubview(resetButton)
}
And here are my functions which are connected to this buttons:
func startButtonAction()
{
blinkAction()
print("START ACTION")
resetButton.isHidden = true
stopButton.isHidden = false
view.layoutSubviews()
}
func resetButtonAction()
{
print("RESET ACTION")
blinkAction()
}
func stopButtonAction()
{
print("STOP ACTION")
blinkAction()
resetButton.isHidden = false
stopButton.isHidden = true
view.layoutSubviews()
}
I add layoutSubviews method but it don't help. I also tried use self before name of button which is hidden but i also doesn't work. Any advice?

You can create button using computed properties with lazy keywoard
lazy var stopButton: UIButton = {
let stopButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100))
stopButton.backgroundColor = .yellow
stopButton.setTitle("Stop", for: .normal)
stopButton.addTarget(self, action: #selector(stopButtonAction), for: .touchUpInside)
stopButton.layer.cornerRadius = 50
stopButton.isHidden = true
stopButton.layer.masksToBounds = true
return stopButton
}()
lazy var resetButton: UIButton = {
let resetButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100))
resetButton.backgroundColor = .red
resetButton.setTitle("Reset", for: .normal)
resetButton.addTarget(self, action: #selector(resetButtonAction), for: .touchUpInside)
resetButton.layer.cornerRadius = 50
resetButton.layer.masksToBounds = true
return resetButton
}()
lazy var startButton: UIButton = {
let startButton = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
startButton.backgroundColor = .green
startButton.setTitle("Start", for: .normal)
startButton.addTarget(self, action: #selector(startButtonAction), for: .touchUpInside)
startButton.layer.cornerRadius = 50
startButton.layer.masksToBounds = true
return startButton
}()

The computed properties startButton, stopButton, resetButton are not the SAME items as the ones added as subviews. Computed properties do not store a value, they give you methods to get values and set changes. So the actions you take there with isHidden do not apply to your view.

The reason of isHidden property not working well is that when your computed property variable you get a new instance of UIButton rather than old one. If you want to want to use computed properties then you should create lazy properties. So whenever you call property variable you'll get old one instance of UIButton.

Your properties always create new buttons, so your references will not be the same. When you set the isHidden, your button will be re-created so you set the isHidden propery of another button that is not added to bottomView.
Just use plain properties:
var stopButton: UIButton!
var resetButton: UIButton!
var startButton: UIButton!
Create the buttons:
func createButtons() {
let stopButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100))
stopButton.backgroundColor = .white
stopButton.setTitle("Stop", for: .normal)
stopButton.addTarget(self, action: #selector(stopButtonAction), for: .touchUpInside)
stopButton.layer.cornerRadius = 50
stopButton.layer.masksToBounds = true
stopButton.isHidden = true
let resetButton = UIButton(frame: CGRect(x: 220, y: 50, width: 100, height: 100))
resetButton.backgroundColor = .red
resetButton.setTitle("Reset", for: .normal)
resetButton.addTarget(self, action: #selector(resetButtonAction), for: .touchUpInside)
resetButton.layer.cornerRadius = 50
resetButton.layer.masksToBounds = true
let startButton = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
startButton.backgroundColor = .green
startButton.setTitle("Start", for: .normal)
startButton.addTarget(self, action: #selector(startButtonAction), for: .touchUpInside)
startButton.layer.cornerRadius = 50
startButton.layer.masksToBounds = true
}
Add as subviews:
bottomView.addSubview(stopButton)
bottomView.addSubview(startButton)
bottomView.addSubview(resetButton)
And use them:
resetButton.isHidden = false
stopButton.isHidden = true

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 create Onboarding\Walkthrough swift

I'm trying to create a welcome onboarding for the first time users but none of the views are loafing in the simulator, all I'm getting is a red background when the onboardingVC gets presented. Can anyone see the issue as to why the titles, buttons, and images won't appear?
This is the message I'm getting in the console:
Warning: Attempt to present <EMA.WalkthroughVC: 0x7faa2401e5b0> on <EMA.HomeVC: 0x7faa22407e00> whose view is not in the window hierarchy!
FOUND ALL!!
let holderView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = true
view.backgroundColor = .darkGray
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
view.backgroundColor = UIColor.red
configure()
}
private func configure() {
let scrollView = UIScrollView(frame: holderView.bounds)
holderView.addSubview(scrollView)
let titles = ["Hi","Welcome","real nigga"]
for x in 0..<3 {
let pageView = UIView(frame: CGRect(x: CGFloat(x) * holderView.frame.size.width, y: 0, width: holderView.frame.size.width, height: holderView.frame.size.height))
scrollView.addSubview(pageView)
let label = UILabel(frame: CGRect(x: 10, y: 10, width: pageView.frame.size.width-20, height: 120))
let imageView = UIImageView(frame: CGRect(x: 10, y: 10+120+10, width: pageView.frame.size.width-20, height: pageView.frame.size.height - 60 - 130 - 15))
let button = UIButton(frame: CGRect(x: 10, y: pageView.frame.size.height - 60, width: pageView.frame.size.width-20, height: 50))
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
pageView.addSubview(label)
label.text = titles[x]
imageView.contentMode = .scaleAspectFill
imageView.image = UIImage(named: "BankCard\(x)")
pageView.addSubview(imageView)
button.setTitleColor(.red, for: .normal)
button.backgroundColor = .black
button.setTitle("Continue", for: .normal)
if x == 2 {
button.setTitle("Get Started", for: .normal)
}
button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
pageView.addSubview(button)
}
}
#objc func didTapButton(_ button: UIButton) {
}
}
"whose view is not in the window hierarchy"
you didn't add the views that you created to the main view try to add the subviews to the main by using this one
self.view.addSubview(holderView)
also don't forget to add the frame for the holder view like that
UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
view.translatesAutoresizingMaskIntoConstraints = true
and inside the view did load
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
self.view.addSubview(holderView)
}

The size of the UIButton doesn't change - Swift

I'm trying to setup the custom navigation bar in my iOS app, but .frame = CGRect(...) doesn't change the size of buttons that I added to it. Buttons appeared inside the navigation bar, but they have wrong size, or maybe constraints, I don't know.
// Setting up the Navigation Bar
private func setupNavigationBar() {
// Remove the shadow under the Navigation Bar
self.navigationController?.navigationBar.shadowImage = UIImage()
let addButton = UIButton(type: .system)
addButton.setImage(UIImage(named: "ic_add")?.withRenderingMode(.alwaysOriginal), for: .normal)
addButton.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
let settingsButton = UIButton(type: .system)
settingsButton.setImage(UIImage(named: "ic_settings")?.withRenderingMode(.alwaysOriginal), for: .normal)
settingsButton.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: settingsButton), UIBarButtonItem(customView: addButton)]
}
(I call this function inside the viewDidLoad())
Here you can see the result on my iPhone:
This is work for me. Please try it. (iOS 11)
private func setupNavigationBar() {
// Remove the shadow under the Navigation Bar
self.navigationController?.navigationBar.shadowImage = UIImage()
let addButton = UIButton(type: .custom)
addButton.setImage(UIImage(named: "ic_add")?.withRenderingMode(.alwaysOriginal), for: .normal)
addButton.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
let settingsButton = UIButton(type: .custom)
settingsButton.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
settingsButton.setImage(UIImage(named: "ic_settings")?.withRenderingMode(.alwaysOriginal), for: .normal)
let menuBarItem = UIBarButtonItem(customView: settingsButton) // new line
let currWidth = menuBarItem.customView?.widthAnchor.constraint(equalToConstant: 10) // new line
currWidth?.isActive = true // new line
let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 10) // new line
currHeight?.isActive = true // new line
navigationItem.rightBarButtonItems = [
menuBarItem, // modify
UIBarButtonItem(customView: addButton)]
}
Try to change the button type
Replace .system to .custom .
Also check the size of original image if it is very large.

Put two (side by side) buttons as a right side overlay of an UITextField

(Swift3/iOS10)
I'm trying to put two buttons (save/cancel) inside of a UITextField using the rightView property. My basic setup code (called from viewDidLoad) is as follows:
private func accesorizeRenameField() {
let saveButton = UIButton(type: .system)
saveButton.setImage(#imageLiteral(resourceName: "buttonCheckmark"), for: .normal)
saveButton.addTarget(self, action: #selector(renameSave), for: .touchUpInside)
saveButton.tintColor = UIColor(white: 0.15, alpha: 1)
let cancelButton = UIButton(type: .system)
cancelButton.setImage(#imageLiteral(resourceName: "buttonX"), for: .normal)
cancelButton.addTarget(self, action: #selector(renameCancel), for: .touchUpInside)
cancelButton.tintColor = UIColor(227, 34, 60, 1)
let container = UIView()
container.addSubview(saveButton)
container.addSubview(cancelButton)
container.translatesAutoresizingMaskIntoConstraints = false
container.backgroundColor = UIColor.cyan
saveButton.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
cancelButton.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
saveButton.heightAnchor.constraint(equalTo: container.heightAnchor).isActive = true
cancelButton.heightAnchor.constraint(equalTo: container.heightAnchor).isActive = true
saveButton.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive = true
saveButton.trailingAnchor.constraint(equalTo: container.centerXAnchor).isActive = true
cancelButton.leadingAnchor.constraint(equalTo: container.centerXAnchor).isActive = true
cancelButton.trailingAnchor.constraint(equalTo: container.trailingAnchor).isActive = true
self.renameField.rightView = container
self.renameField.rightViewMode = .always
}
Later on, when I expose the rename field through some animation, I call
self.renameField.rightView?.bounds = CGRect(x: 0, y: 0, width: self.renameField.bounds.height * 2, height: self.renameField.bounds.height)
self.renameField.rightView?.setNeedsLayout()
"rightView.frame \(self.renameField.rightView?.frame)".print()
self.renameField.rightView?.subviews.enumerated().forEach() {index,child in
"child \(index) frame \(child.frame)".print()
}
However, I cannot get the buttons to show up. The cyan background (for debugging) shows up, but actually is square (it should be a 2:1 rectangle). The print shows that the subviews have frames of size 0. What is the right/idiomatic way to do this? I feel like I'm just throwing hammers here...
Create UIButton as type of .custom instead of .system.
And as you already know renameField's height in viewDidLoad, you don't need to bother with constraint.
The code below is enough.
override func
viewDidLoad() {
super.viewDidLoad()
let wSize = renameField.frame.size.height - 2
let saveButton = UIButton( type: .custom )
saveButton.setImage( #imageLiteral(resourceName: "buttonCheckmark"), for: .normal )
saveButton.addTarget( self, action: #selector(renameSave), for: .touchUpInside )
saveButton.frame = CGRect( x: 0, y: 0, width: wSize, height: wSize )
let cancelButton = UIButton( type: .custom )
cancelButton.setImage( #imageLiteral(resourceName: "buttonX"), for: .normal )
cancelButton.addTarget( self, action: #selector(renameCancel), for: .touchUpInside )
cancelButton.frame = CGRect( x: wSize, y: 0, width: wSize, height: wSize )
let wV = UIView()
wV.frame = CGRect( x:0, y:0, width: wSize * 2, height: wSize )
wV.addSubview( saveButton )
wV.addSubview( cancelButton )
renameField.rightView = wV;
renameField.rightViewMode = .always;
}

Resources