【问题标题】:How to add property to UIButton?如何向 UIButton 添加属性?
【发布时间】:2016-08-23 06:29:44
【问题描述】:

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

我正在尝试为layer.shadowColorlayer.shadowRadius 等键盘键添加属性。 我有一个错误

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

如何解决这个问题?

这是我的代码keyboardViewController.swift

导入 UIKit

类键盘视图控制器: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 func keyPressed(sender: UIButton) { } 我添加了一些按钮并想为按钮添加阴影。像使用 layer.shadowColor
  • 在 keyPressed(sender: UIButton) 内部,编写添加阴影的代码,例如我添加了一些用于添加边框颜色的代码.. sender.layer.borderColor = UIColor.redColor().CGColor sender。 layer.borderWidth = 2.0 希望有帮助。
  • 出现错误。我上次得到的也是一样:c..'顺便说一句谢谢!
  • 尝试输入'let button = sender as Button!'和 func keyPressed(sender: UIButton) 中的 'button.layer.....'

标签: ios swift uibutton quartz-core


【解决方案1】:

我认为为了给按钮应用一些样式,你需要这个按钮的出口。 现在,据我所知,您正在尝试将样式应用于从@IBAction 到发件人的按钮,这不是正确的方法。 尝试为视图控制器中的按钮创建一个插座,然后从 viewDidLoad 方法中应用样式。

我希望这很清楚,但如果您想要更具体的答案,您需要向我们展示您的尝试,例如粘贴您在视图控制器中的代码

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

无论如何,据我所知,我认为您没有编写此代码:可能您正在学习教程或维护其他人的代码。没关系,但我建议你学习一门关于使用 Swift 进行 iOS 开发的入门课程,比如 Udacity 的课程,恕我直言(https://www.udacity.com/course/intro-to-ios-app-development-with-swift--ud585

【讨论】:

  • 我很困惑。你是说键盘视图控制器吗?
【解决方案2】:

如果您尝试使用 QuartzCore 框架格式化您的 UIButton,您需要先导入它:

import QuartzCore

然后您将能够访问这些成员。

例如(最新的swift3代码):

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

如果您需要尽快应用样式,请考虑将此代码放入 viewDidLoadviewDidAppear 方法中:

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

【讨论】:

  • 无论如何我无法想象它会是什么样子,但代码应该是这样工作的:)
【解决方案3】:

似乎您不是在尝试向按钮“添加属性”,而是向接受按钮作为参数的闭包。

把它变成这样:

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

【讨论】:

  • 嗯我添加了按钮 :o .. 有没有办法向 uibutton 添加属性?
  • @IBAction func keyPressed(sender: UIButton) { } 这都是关于我想为 :c 添加一些东西的按钮 ...
  • 这不是相关代码。显示您尝试设置 layer.shadowColor 的代码
  • 我认为我的问题是我正在尝试向按钮添加东西,这是 IBAction 而不是 IBOutlet ;-;我试图在 IBAction func 按键内设置 layer.shadowColor。不正确吧?
  • 您只需要显示您的代码。我不能不看就告诉你
猜你喜欢
  • 2011-07-26
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
相关资源
最近更新 更多