Search code examples
iosswiftuibuttonquartz-core

How to add property to UIButton?


thanks for all help:)! fixed it using iboutlet collection and add properies on viewDidLoad

I'm trying to add properties to keyboard keys like layer.shadowColor or layer.shadowRadius. I got an error

 'Value of type '(UIButton)' -> () has no member 'layer'

how to fix this ?

this is my code keyboardViewController.swift

import UIKit

class KeyboardViewController: UIInputViewController { var newKeyboardView: UIView!

@IBAction func keyPressed(sender: UIButton) {

}

@IBOutlet var nextKeyboardButton: UIButton!

override func updateViewConstraints() {
    super.updateViewConstraints()

    // Add custom view sizing constraints here
}

override func viewDidLoad() {
    super.viewDidLoad()

    loadInterface()

}

func loadInterface() {
    // load the nib file
    let keyboardNib = UINib(nibName: "newKeyboard", bundle: nil)
    // instantiate the view
    newKeyboardView = keyboardNib.instantiateWithOwner(self, options: nil)[0] as! UIView

    // add the interface to the main view
    view.addSubview(newKeyboardView)

    // copy the background color
    view.backgroundColor = newKeyboardView.backgroundColor
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated
}

override func textWillChange(textInput: UITextInput?) {
    // The app is about to change the document's contents. Perform any preparation here.
}

override func textDidChange(textInput: UITextInput?) {
    // The app has just changed the document's contents, the document context has been updated.

    var textColor: UIColor
    let proxy = self.textDocumentProxy
    if proxy.keyboardAppearance == UIKeyboardAppearance.Dark {
        textColor = UIColor.whiteColor()
    } else {
        textColor = UIColor.blackColor()
    }
    self.nextKeyboardButton.setTitleColor(textColor, forState: .Normal)
}

}


Solution

  • I think that in order to apply some style to the button, you need an outlet to this button. Right now, from what I can understand, you are trying to apply styles to the button from the @IBAction to the sender, which is not the proper way to do it. Try to make an outlet to the button in the view controller and then to apply the styles from within the viewDidLoad method.

    I hope this is clear, but if you want a more specific answer you need to show us what you tried, for example pasting the code you have in the view controller

    EDIT: Based on the code you post, the keyboard is a Nib you instantiate from loadInterface(). I don't have a clear vision of the whole thing with only this piece of code, but it seems to me that you are trying to apply some styles to every key button of a keyboard view. Unfortunately this really depends on how the keyboard is implemented, can you provide some more details?

    Anyway, from what I see I think you didn't write this code: probably you are following a tutorial or maintaining someone else's code. That's ok, but I suggest you to follow a an introduction course to iOS development with Swift, like the Udacity's one, which is fantastic IMHO (https://www.udacity.com/course/intro-to-ios-app-development-with-swift--ud585)