Ios 如何将属性添加到UIButton?
谢谢你的帮助:)!使用iboutlet集合修复了此问题,并在viewDidLoad上添加了属性 我正在尝试向键盘键添加属性,如Ios 如何将属性添加到UIButton?,ios,swift,uibutton,quartz-core,Ios,Swift,Uibutton,Quartz Core,谢谢你的帮助:)!使用iboutlet集合修复了此问题,并在viewDidLoad上添加了属性 我正在尝试向键盘键添加属性,如layer.shadowColor或layer.shadowRadius。 我犯了个错误 'Value of type '(UIButton)' -> () has no member 'layer' 如何解决这个问题 这是我的代码keyboardViewController.swift 导入UIKit 类KeyboardViewController:UIInp
layer.shadowColor
或layer.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)
}
}似乎您试图“添加属性”的不是按钮,而是接受按钮作为参数的闭包 这样做:
nextKeyboardButton.layer.shadowColor = UIColor.redColor.cgColor
nextKeyboardButton.layer.shadowRadius = 5.0
如果尝试使用QuartzCore框架格式化UIButton,则需要首先导入它:
import QuartzCore
然后您将能够访问这些成员
例如(最新的swift3代码):
如果您需要更快地应用样式,请尝试将此代码放入<强> VIELDHOLD <<强>或>强> VIEDIDALVIUTION/STATION >方法:
self.nextKeyboardButton.backgroundColor = UIColor.red
self.nextKeyboardButton.layer.shadowColor = UIColor.black.cgColor
self.nextKeyboardButton.layer.shadowRadius = 1.0
self.nextKeyboardButton.layer.cornerRadius = 4.0
我认为,为了给按钮应用某种风格,你需要一个按钮的插座。 现在,据我所知,您正试图将样式应用于从@iAction到发件人的按钮,这不是正确的方法。 尝试为视图控制器中的按钮创建一个出口,然后从viewDidLoad方法中应用样式 我希望这是清楚的,但是如果您想要一个更具体的答案,您需要向我们展示您的尝试,例如粘贴视图控制器中的代码 编辑: 根据您发布的代码,键盘是从loadInterface()实例化的Nib。我对这段代码没有一个清晰的了解,但在我看来,您正在尝试将一些样式应用于键盘视图的每个按键。不幸的是,这真的取决于键盘是如何实现的,你能提供更多的细节吗
无论如何,从我看到的情况来看,我认为您没有编写这段代码:可能您正在遵循教程或维护其他人的代码。没关系,但我建议你学习一门关于iOS开发的Swift入门课程,就像Udacity的一样,这是一门非常棒的课程,IMHO()你能展示代码吗?@IBAction func keyPressed(sender:UIButton){}我添加了一些按钮,想给按钮添加阴影。像使用layer.shadowColorInside按键(sender:UIButton)一样,编写用于添加阴影的代码,例如,我添加了一些用于添加边框颜色的代码。。sender.layer.borderColor=UIColor.redColor().CGColor sender.layer.borderWidth=2.0希望有帮助。出现错误。这和我上次得到的一样:c…'顺便说一句,谢谢!尝试键入“let button=发件人作为按钮!”和“button.layer…”内按func键(发送者:UIButton)嗯,我添加了按钮:o。。有没有办法向uibutton添加属性?@IBAction func按键按下(发送方:uibutton){}这是关于我想为其添加内容的按钮的所有内容:c…这不是相关代码。显示您试图设置layer.shadowcolor的代码我想我的问题是,我试图向按钮添加内容,它是iAction而不是iOutlet;-;我试图在iAction func按键内设置layer.shadowColor。这是不正确的,对吗?你所需要的只是显示你的代码。我不能不看就告诉你我很困惑。你是说keyboardviewcontroller吗?无论如何,我无法想象它会是什么样子,但代码应该是这样工作的:)
self.nextKeyboardButton.backgroundColor = UIColor.red
self.nextKeyboardButton.layer.shadowColor = UIColor.black.cgColor
self.nextKeyboardButton.layer.shadowRadius = 1.0
self.nextKeyboardButton.layer.cornerRadius = 4.0