2016-08-23 2 views
2

모든 도움에 감사드립니다 :)! iboutlet 컬렉션을 사용하여 고정시키고 viewDidLoad에 속성을 추가하십시오.UIButton에 속성을 추가하는 방법은 무엇입니까?

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

} 당신이 아니라 인수로 버튼을 받아들이는 폐쇄로가 아닌 버튼에 "속성을 추가"하려는처럼

+0

과 같이 Swift로 iOS 개발에 대한 소개 과정을 따르는 것이 좋습니다. 코드를 표시 할 수 있습니까? – good4pc

+0

@IBAction func keyPressed (발신자 : UIButton) { } 버튼을 추가하고 단추에 그림자를 추가하고 싶습니다. like with layer.shadowColor –

+0

keyPressed (보낸 사람 : UIButton) 안에 그림자를 추가하는 코드를 작성합니다. 예를 들어 테두리 색상 추가를위한 코드를 추가했습니다. sender.layer.borderColor = UIColor.redColor(). CGColor sender.layer .borderWidth = 2.0 도움이 되길 바랍니다. – good4pc

답변

1

버튼에 스타일을 적용하려면이 버튼의 콘센트가 필요하다고 생각합니다. 지금 당장 이해할 수있는 바에 따르면, 버튼에 @IBAction에서 보낸 사람에게 스타일을 적용하려고합니다. 이는 올바른 방법이 아닙니다. 보기 컨트롤러의 단추 콘센트를 만든 다음 viewDidLoad 메서드 내에서 스타일을 적용 해 봅니다.

나는이 분명 희망,하지만 당신은 좀 더 구체적인 대답을 원한다면 당신은 뷰 컨트롤러에이 코드를 붙여 당신은 예를 들어, 시도 무엇을 우리에게 보여줄 필요가

편집 : 에 기초 코드를 게시하면 키보드는 loadInterface()에서 인스턴스화 한 Nib입니다. 이 코드만으로 모든 것에 대한 명확한 비전은 없지만 키보드보기의 모든 키 버튼에 스타일을 적용하려고하는 것 같습니다. 불행히도 이것은 실제로 키보드가 구현되는 방법에 달려 있습니다. 좀 더 자세한 정보를 제공 할 수 있습니까?

어쨌든, 나는이 코드를 작성하지 않았다고 생각합니다. 아마도 자습서를 따르거나 다른 사람의 코드를 유지하고있을 것입니다.괜찮아요.하지만 UHACITY의 것 같은 환상적인 IMHO (https://www.udacity.com/course/intro-to-ios-app-development-with-swift--ud585)

+0

저는 혼란 스럽습니다. keyboardviewcontroller를 의미합니까? –

1

보인다.

하면이처럼 확인 : 당신은 QuartzCore 프레임 워크와 함께있는 UIButton를 포맷하려고하면

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

hmm 나는 버튼을 추가 : o .. uibutton에 속성을 추가하는 방법이 있습니까? –

+0

코드를 표시해야합니다. –

+0

@IBAction func keyPressed (발신자 : UIButton) { 여기에 대한 정보는 다음과 같습니다. –

1

것은, 당신이 먼저 가져해야합니다 :

import QuartzCore 

는 그런 다음에 액세스 할 수 있습니다 그 회원들.

예 (최신 swift3 코드)의 경우 :

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

은 빨리 당신의 스타일을 적용해야 할 경우
@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 방법이 코드를 넣어 생각하려고

+0

어쨌든 어떻게 생겼는지 상상할 수 없지만 코드는 다음과 같이 작동해야합니다. – pedrouan

+0

감사합니다. –

관련 문제