2016-08-23 140 views
2

感謝所有幫助:)!使用iboutlet集合修復它並在viewDidLoad上添加適當的東西如何將屬性添加到UIButton?

我試圖給鍵盤按鍵添加屬性,如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) 
} 

}

+0

你可以顯示代碼嗎? – good4pc

+0

@IBAction func keyPressed(sender:UIButton){ }我添加了一些按鈕,並希望爲按鈕添加陰影。像使用layer.shadowColor –

+0

裏面keyPressed(發件人:UIButton),編寫代碼添加陰影,例如我已添加一些代碼添加邊框顏色.. sender.layer.borderColor = UIColor.redColor()。CGColor sender.layer .borderWidth = 2.0希望它有幫助。 – good4pc

回答

1

我認爲,爲了將一些風格應用到按鈕上,您需要一個出口到這個按鈕。 現在,從我所能理解的情況來看,您試圖將樣式應用於從@IBAction到發件人的按鈕,這不是正確的方法。 嘗試使視圖控制器中的按鈕出口,然後應用viewDidLoad方法內的樣式。

我希望這是明確的,但如果你想更具體的答案,你需要告訴我們你嘗試過什麼,例如粘貼您可以在視圖控制器具有代碼

編輯:基於 你發佈的代碼,鍵盤是一個你從loadInterface()實例化的Nib。我只用這段代碼並沒有清楚地看到整個事物,但在我看來,您正試圖將某些樣式應用於鍵盤視圖的每個按鍵。不幸的是,這真的取決於鍵盤是如何實現的,你能否提供更多的細節?

無論如何,從我看到的我認爲你沒有寫這段代碼:可能你正在關注教程或維護別人的代碼。沒關係,但我建議你遵循iOS開發的Swift簡介課程,就像Udacity的一個,這是一個夢幻般的恕我直言(https://www.udacity.com/course/intro-to-ios-app-development-with-swift--ud585

+0

我很困惑。你的意思是keyboardviewcontroller? –

1

好像你正試圖「添加屬性」不是按鈕,而是,它接受一個按鈕作爲參數封閉。

讓這樣的:

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

嗯我添加按鈕:o ..有沒有辦法給uibutton添加屬性? –

+0

你需要顯示的代碼 –

+0

@IBAction func keyPressed(發件人:UIButton){ } } 這是關於按鈕,我想添加一些東西:c ... –

1

如果您試圖格式化與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 
} 

在您需要申請您的樣式更快的情況下,儘量考慮把這個代碼放到viewDidLoad中viewDidAppear方法:

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

無論如何我無法想象它將如何,但代碼應該像這樣:) – pedrouan

+0

感謝您的幫助:) –