All objects disappear in view controller (main storyboard) when I use custom #IBDesignable UITextfield class. (Xcode 10, Swift 4.2) - swift

All objects disappear in view controller (main storyboard) when I use custom #IBDesignable UITextfield class. Main storyboard looks like this:
(Xcode 10, Swift 4.2)
As you see, it shows an error also. But Xcode shows 'Build Succeeded' when I run the app.
I use custom #IBDesignable UITextfield class like this below:
import UIKit
import AKMaskField
#IBDesignable
class MyTextFieldStyle: AKMaskField {
#IBInspectable var secureEntry : Bool = false
#IBInspectable var myBorderColor : UIColor = UIColor.white {
didSet {
self.layer.borderColor = myBorderColor.cgColor
}
}
#objc func secureButtonPressed (sender: UIButton) {
self.secureEntry = !self.secureEntry
self.isSecureTextEntry = self.secureEntry
}
override func awakeFromNib() {
super.awakeFromNib()
if self.secureEntry {
let btn = UIButton(type: .custom)
btn.setImage(#imageLiteral(resourceName: "eye-17-glyph-16"), for: .normal)
btn.addTarget(self, action: #selector(self.secureButtonPressed(sender:)), for: .touchUpInside)
btn.sizeToFit()
btn.frame = CGRect(x: self.frame.size.width-(btn.frame.size.width+5), y: (self.frame.size.height-btn.frame.size.height)/2, width: btn.frame.size.width, height: btn.frame.size.height)
self.addSubview(btn)
}
self.layer.borderColor = self.myBorderColor.cgColor
}
}
It becomes fine when I close and open Xcode. It looks like a bug. This was working fine.

Related

UITextField clear button color Swift

How can I change clear button color or background color in UITextField. I tried get subviews of textField, but in subviews not clear button.
import UIKit
class CustomTextField: UITextField {
override func layoutSubviews() {
super.layoutSubviews()
for view in subviews {
if let button = view as? UIButton {
button.setImage(button.image(for: .normal)?.withRenderingMode(.alwaysTemplate), for: .normal)
button.tintColor = .white
}
}
}
}

Adding custom buttons to UITabBarController (adding button in the center)

i'm trying to make a custom tab bar with some images i made and i'm having some trouble. I'm trying to add a button to the tab bar and it seems like i can't do it. I want to do something like this:
Then adding some animations to that button. How can i go about adding that button? Do i need to subclass UITabBarController? Thank you!
In TabbarController class, add this code
override func viewDidLoad() {
super.viewDidLoad()
setupMiddleButton()
}
// MARK: - AddButton
func setupCenterButton() {
let centerButton = UIButton(frame: CGRect(x: 0, y: 10, width: 45, height: 45))
var centerButtonFrame = centerButton.frame
centerButtonFrame.origin.y = (view.bounds.height - centerButtonFrame.height) - 2
centerButtonFrame.origin.x = view.bounds.width/2 - centerButtonFrame.size.width/2
centerButton.frame = centerButtonFrame
centerButton.layer.cornerRadius = 35
view.addSubview(centerButton)
centerButton.setBackgroundImage(#imageLiteral(resourceName: "tabPost"), for: .normal)
centerButton.addTarget(self, action: #selector(centerButtonAction(sender:)), for: .touchUpInside)
view.layoutIfNeeded()
}
// MARK: - Center button Actions
#objc private func centerButtonAction(sender: UIButton) {
selectedIndex = 2
}
It will work.. :)
You should implement the delegate method of UITabBarControllerDelegate:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController == (self.tabBarController?.viewControllers?[theIndexOfTheButton])! {
// do my stuffs here
return false
}
return true
}
Don't forget to set self.tabBarController?.delegate = self

How to get textFields from static cells in UITableViewController? Swift

My View hierarchy looks like this:
ElevethViewController of type UIViewController
Container View
ManagedTableEleventhViewController of type UITableViewController embedded in Container View
ManagedTableEleventhViewController contains 4 static cells containing 1 textField each and one empty static cell.
class ManagedTableEleventhViewController: UITableViewController,UITextFieldDelegate {
var hasText:Bool!
#IBOutlet weak var fullName: UITextField!
#IBOutlet weak var flatNumber: UITextField!
#IBOutlet weak var streetAddress: UITextField!
#IBOutlet weak var phoneNumber: UITextField!
//checkValue takes ELViewController parameter so that segue can be
//performed when button is touched in EleventhViewController
func checkValue(ELViewController:EleventhViewController) {
//loop through the textfields and check if they have text
for case let textField as UITextField in viewController.view.subviews {
//print is not executed meaning loop is not performed
print("some text")
if textField.text == "" {
self.hasText = false
textField.layer.borderColor = UIColor.red.cgColor
} else {
print("true value in for loop")
self.hasText = true
performSegue(withIdentifier: "elevethToTwelveth", sender: ELViewController)
}
}//end of for loop
}
class EleventhViewController: UIViewController {
var nextButtonOutlet:UIButton!
override func viewDidLoad() {
super.viewDidLoad()
//create button programmatically
var button = UIButton(type: UIButtonType.custom) as UIButton
button = UIButton(frame: CGRect(x: 0, y: 637, width: 375, height: 50))
button.titleLabel?.textColor = UIColor.white
button.backgroundColor = UIColor(colorLiteralRed: 117/255, green: 232/255, blue: 0, alpha: 1)
button.setTitle("Next", for: .normal)
button.addTarget(self, action: #selector(EleventhViewController.nextButton), for: .touchUpInside)
self.view.addSubview(button)
self.nextButtonOutlet = button
}
func nextButton(sender: UIButton) {
//create instance of tableView
let managedTable = ManagedTableEleventhViewController()
managedTable.checkValue(viewController: self)
} //end of EleventhViewController class
Well first I can give you an answer that might satisfy you and fix your loop but I would recommend not doing it that way to alter your textfields. I would recommend doing it in cellForRow even though they may be static cells. Depending on your view setup in the cells it would look like this if the textfield is added directly to the cells and not to another view.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("Testing")
for cell in tableView.visibleCells{
for sub in cell.contentView.subviews{
if sub is UITextField{
print("Textfield")
}
}
}
}
Just to follow up, if this is for validation you should'nt be only checking "" case, because you allow " ", " " etc. Use isEmpty, it should work better, if you only want to check existence of text.
Also you dont have to extract fields from subviews as you already have properties, i'm not sure if you have any other reason for this logic though.
Edit. Ooops, i just noticed your checking for textfields in a controller which does not have any visible fields, so normally your check never passes.
I think you should'nt even validate textfields for one class in another class, unless its a class handling textfield validation in general.
In EleventhViewController you have no textfields, so nothing to validate.

Swift: connect a static button with a static method

Say I have a static button member variable, as well as a static method which handles its click. How can I connect the button with the method? I couldn't figure out how to get addTarget to work in this case:
private static let my_button: UIButton = {
let button = UIButton(type: .system)
...
button.addTarget(???, action: #selector(handleButtonClick), for: .touchUpInside)
return button
}()
private static func handleButtonClick() {
...
}
Could I put UIApplication.shared.keyWindow!.rootViewController in place of ????
Details
xCode 8.2.1, Swift 3
Full sample
import UIKit
class Button:NSObject {
class func createButton() -> UIButton {
let button = UIButton(type: .system)
button.frame = CGRect(x: 40, y: 40, width: 200, height: 40)
button.setTitle("Text", for: .normal)
button.addTarget(self, action: #selector(Button.handleButtonClick), for: .touchUpInside)
return button
}
class func handleButtonClick() {
print("Click")
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.addSubview(Button.createButton())
}
}
Result
You have to have an actual object - an instance of your class - as the button's target. It might be possible to pass the class object, but that's not how it's supposed to work. The button basically expects its target to be an object that inherits from NSResponder.

Extension vs Subclass. How to explain different behavior when I set background color for UIButton

I want to change background color for UIButton in highlighted state.
I use two different ways.
Case #1 with extension:
extension UIButton {
override open func awakeFromNib() {
super.awakeFromNib()
backgroundColor = .green
setTitleColor(.white, for: .normal)
}
override open var isHighlighted :Bool {
didSet{
if isHighlighted {
backgroundColor = .red
} else {
backgroundColor = .green
}
}
}
}
Case #2 with subclass:
class SHButton: UIButton {
override open func awakeFromNib() {
super.awakeFromNib()
backgroundColor = .green
setTitleColor(.white, for: .normal)
}
override open var isHighlighted :Bool {
didSet{
if isHighlighted {
backgroundColor = .red
} else {
backgroundColor = .green
}
}
}
}
I use very similar code, but I see different results in runtime.
In second case button title has not a white color.
Why we can see so different results in runtime?
Updated. This question has been resolved. I'm dummy. I forgot to change button Type to Custom. When I have did it both examples began to work identically.
this should also fix your problem
button.setTitleColor(.white, for: .highlighted)