简体   繁体   中英

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)
}

}

Seems like you're trying to "add property" not to a button, but rather to a closure which accepts a button as an argument.

Make it like this:

nextKeyboardButton.layer.shadowColor = UIColor.redColor.cgColor
nextKeyboardButton.layer.shadowRadius = 5.0

If you try to format your UIButton with QuartzCore framework, you'll need to import it first:

import QuartzCore

Then you will be able to access those members.

For example (latest swift3 code):

@IBAction func keyPressed(sender: UIButton) {
    let button = sender as UIButton!
        button?.backgroundColor = UIColor.red
        button?.layer.shadowColor = UIColor.black.cgColor
        button?.layer.shadowRadius = 1.0
        button?.layer.cornerRadius = 4.0
}

In case you need to apply your styles sooner, try to consider to put this code into viewDidLoad or viewDidAppear methods:

    self.nextKeyboardButton.backgroundColor = UIColor.red
    self.nextKeyboardButton.layer.shadowColor = UIColor.black.cgColor
    self.nextKeyboardButton.layer.shadowRadius = 1.0
    self.nextKeyboardButton.layer.cornerRadius = 4.0

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM