2017-11-16 5 views
0

스크롤되지 않는 :있는 UIScrollView 이것은 단순히 코드에서 할 것입니다 더 이상

enter image description here

하지만 스크롤 할 수 없습니다 :

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    let scroll = UIScrollView() 
    scroll.backgroundColor = .yellow 
    scroll.translatesAutoresizingMaskIntoConstraints = false 

    let leading = NSLayoutConstraint(item: scroll, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0) 
    let trailing = NSLayoutConstraint(item: scroll, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0) 
    let top = NSLayoutConstraint(item: scroll, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0) 
    let bottom = NSLayoutConstraint(item: scroll, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0) 

    view.addSubview(scroll) 
    view.addConstraints([leading, trailing, top, bottom]) 

    let label = UILabel() 
    label.text = "helloo, my new text" 
    label.backgroundColor = .orange 
    label.translatesAutoresizingMaskIntoConstraints = false 

    let leading2 = NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: scroll, attribute: .leading, multiplier: 1, constant: 0) 
    let trailing2 = NSLayoutConstraint(item: label, attribute: .trailing, relatedBy: .equal, toItem: scroll, attribute: .trailing, multiplier: 1, constant: 0) 
    let top2 = NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: scroll, attribute: .top, multiplier: 1, constant: 200) 
    let bottom2 = NSLayoutConstraint(item: label, attribute: .bottom, relatedBy: .equal, toItem: scroll, attribute: .bottom, multiplier: 1, constant: 400) 
    let height2 = NSLayoutConstraint(item: label, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 600) 

    scroll.addSubview(label) 
    label.addConstraint(height2) 
    scroll.addConstraints([leading2, trailing2, top2, bottom2]) 
    scroll.layoutIfNeeded() 
} 

이것은 화면에 보이는 방법이다 그것은 전혀. 왜? 여기서 뭐가 잘못 됐니?

많은 예제와 질문이 있으므로 알고 있지만 한 개도 작동하지 않습니다.

+0

400의 상수 값을 변경 뭔가 UILabel의 및있는 UIScrollView에 주어진 제약 조건에 문제가 있습니다. 수직 스크롤을 UILabel과 함께 수직으로 제한하는 경우. –

+0

무엇이 잘못 되었나요?;) 정말로 이것에 대해 오랫동안 생각합니다 ... –

답변

1

라벨에 잘못된 하단 제약 조건을 지정했습니다. 0

let bottom2 = NSLayoutConstraint(item: label, attribute: .bottom, relatedBy: .equal, toItem: scroll, attribute: .bottom, multiplier: 1, constant: 0) 

enter image description here

+1

그것이 작동합니까;) 왜 중요한가요?;) 0은 좋지만 400은 그렇지 않습니다. –

+0

@ BartłomiejSemańczyk : scrollview는이 정보를 사용하여 내용 크기를 계산하므로 scrollview는 내용 크기가 원래의 프레임 크기보다 큰 경우에만 스크롤합니다. –