2016-08-28 2 views
0

신속하게 작성된 iOS 응용 프로그램의 단추보기 개체를 내 장면에 프로그래밍 방식으로 추가하려고합니다. 여기에 대한 답변을 따르려고합니다. https://stackoverflow.com/a/36263784신속하게 제약 조건을 올바르게 활성화하는 방법

그러나 장면의 버튼을 볼 수 없습니다. 내 viewDidLoad 메서드에서이 메서드를 호출합니다.

func buildLoginButton() -> UIButton 
{ 
    let button = UIButton() 
    button.backgroundColor = .greenColor() 
    button.setTitle("Test Button", forState: .Normal) 
    self.view.addSubview(button) 

    let c1 = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0) 

    let c2 = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0) 

    button.widthAnchor.constraintEqualToAnchor(nil, constant: 200).active = true 
    button.heightAnchor.constraintEqualToAnchor(nil, constant: 100).active = true 

    NSLayoutConstraint.activateConstraints([c1, c2]) 
    view.bringSubviewToFront(button) 

    return button 
} 
+0

디버그 창에서 모호한 레이아웃 제약과 관련하여 경고 메시지가 표시됩니까? – ozgur

+0

'buildLoginButton()'이 호출되도록 보장 했습니까? 메인 스레드에서? – BaseZen

답변

1
let button = UIButton() 
self.view.addSubview(button) 

그만! 이 말을 잊었 :

button.translatesAutoresizingMaskIntoConstraints = false` 

당신은 인터페이스에 넣어 당신이 만드는 모든보기에 이런 말을하고,에 제약을 적용하려고해야합니다.

관련 문제