ios - 如何给UIButton添加属性?

标签 ios swift uibutton quartz-core

感谢所有帮助:)!使用 iboutlet 集合修复它并在 viewDidLoad 上添加属性

我正在尝试向键盘按键添加属性,例如 layer.shadowColorlayer.shadowRadius。 我收到一个错误

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

如何解决这个问题?

这是我的代码keyboardViewController.swift

导入UIKit

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

}

最佳答案

我认为为了对按钮应用某种样式,您需要一个该按钮的导出。 现在,据我所知,您正在尝试将样式应用于从 @IBAction 到发件人的按钮,这不是正确的方法。 尝试在 View Controller 中为按钮创建一个导出,然后从 viewDidLoad 方法中应用样式。

我希望这一点很清楚,但如果您想要更具体的答案,您需要向我们展示您尝试过的内容,例如粘贴您在 View Controller 中的代码

编辑: 根据您发布的代码,键盘是您从 loadInterface() 实例化的 Nib。仅凭这段代码,我对整个事情没有清晰的认识,但在我看来,您正在尝试将某些样式应用于键盘 View 的每个按键。不幸的是,这实际上取决于键盘的实现方式,您能提供更多详细信息吗?

无论如何,从我看来,我认为您没有编写这段代码:可能您正在遵循教程或维护其他人的代码。没关系,但我建议您学习使用 Swift 进行 iOS 开发的入门类(class),例如 Udacity 的类(class),这非常棒,恕我直言 ( https://www.udacity.com/course/intro-to-ios-app-development-with-swift--ud585 )

关于ios - 如何给UIButton添加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39093842/

相关文章:

Swift reduce - 为什么 value 是可选的?

ios - Swift: map View 消失/重新出现后,MapView geocodeAddressString 不起作用

ios - 在用 Swift 编写的 Xcode 中的 iOS 应用程序中的 UITextView 中,将 UIButton 放置在文本的末尾

ios - 无法将类型 '[HKQuantityType?]' 的值转换为指定类型 'Set'

ios - 以编程方式处理 View 调整高度、宽度和边距

ios - 如何检查 ios 设备何时失去与互联网的连接

ios - UIButton 在我不希望它是透明的时候是透明的

ios - 如何在 iOS 的 UITableView 中显示可点击的 WordPress 博客文章标题?

iOS 测试飞行 : Internal Tester not receiving invite

swift - 边框不覆盖背景