2017-09-14 8 views
0

내용을 내 레이블에 추가 할 때 크기를 조정하고 scrollview를 세로로 스크롤 할 수 있도록하는 데 문제가 있습니다. 내 계층 구조는 다음과 같습니다내용에 따라 uiscrollview 크기 조정

my hierarchy

내 생각이 보이지만 :

my view

그래서 나는 텍스트를 얻을 때 시작에 내 코드의 내용으로 라벨을 작성하고 싶습니다 API 호출로부터. 그러나 테스트를 위해 일부 lorem ipsum 텍스트를 하드 코딩하여 작동하도록했습니다. 따라서 내 컨텐츠가 레이블에 추가되고 UILabels의 크기가 조정되고 레이블이 포함 된 뷰의 크기가 조정됩니다. 라벨의 텍스트가 더 길면 스크롤 할 수 있도록 scrollview 내용의 크기를 조정하는 데 문제가 있습니다. 그것이 내가 텍스트 길이에 따라 내 라벨 및보기 크기 조정을 할 수 있었던 유일한 방법 이었기 때문에 나는 또한 PureLayout을 사용하고

import UIKit 
import PureLayout 

class QuestionAndAnswerViewController: UIViewController { 

@IBOutlet var menuButton: UIBarButtonItem! 
@IBOutlet var questionView: UIView! 
@IBOutlet var questionLabel: UILabel! 
@IBOutlet var questionContentLabel: UILabel! 
@IBOutlet var answerView: UIView! 

@IBOutlet var odgovorLabel: UILabel! 
@IBOutlet var answerLabel: UILabel! 
@IBOutlet var signLabel: UILabel! 

@IBOutlet var lineView: UIView! 

@IBOutlet var scrollView: UIScrollView! 
@IBOutlet var contentView: UIView! 

var yPosition: CGFloat = 0.0 
var contentSize: CGFloat = 0.0 
var attrText = NSMutableAttributedString() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    scrollView.setNeedsLayout() 
    scrollView.layoutIfNeeded() 
    scrollView.translatesAutoresizingMaskIntoConstraints = false 


    let paragraphStyle = NSMutableParagraphStyle() 
    paragraphStyle.lineSpacing = 5 
    paragraphStyle.alignment = .justified 

    let paragraphStyle2 = NSMutableParagraphStyle() 
    paragraphStyle2.lineSpacing = 5 
    paragraphStyle2.alignment = .left 


    if self.revealViewController() != nil { 
     self.revealViewController().frontViewShadowRadius = 5.0 
     self.revealViewController().frontViewShadowOpacity = 0.25 
     menuButton.target = self.revealViewController() 
     menuButton.action = #selector(SWRevealViewController.rightRevealToggle(_:)) 
     self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) 
    } 

    var text = "To je prvo vprašanje? Kako dolgi je lahko ta tekst da se poravna" 
    attrText = NSMutableAttributedString(string: text) 
    attrText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle2, range: NSMakeRange(0, attrText.length)) 
    questionLabel.attributedText = attrText 


    text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut quis mi nisi. Etiam nec augue id dui blandit ornare. Nulla auctor, purus vel tincidunt ultricies, enim turpis molestie augue, nec mattis libero ante mattis mauris. Suspendisse posuere, velit posuere viverra feugiat, nulla justo bibendum nisi, nec ultricies lorem enim in nisl. Nunc sit amet quam mollis, faucibus felis eu, posuere dui. Sed vel mattis neque. Fusce elementum at nisl ut volutpat. Nam placerat consequat mi in lacinia. Morbi ut est tristique, efficitur est a, faucibus erat. Suspendisse et ligula ac lacus porttitor pretium ut vehicula felis." 
    attrText = NSMutableAttributedString(string: text) 

    attrText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attrText.length)) 
    questionContentLabel.attributedText = attrText 


    text = "Cras auctor ullamcorper ullamcorper. Vestibulum dignissim quis risus eget congue. Sed et diam libero. Phasellus non lectus sem. Cras auctor ullamcorper ullamcorper. Vestibulum dignissim quis risus eget congue. Sed et diam libero. Phasellus non lectus sem. Cras auctor ullamcorper ullamcorper. Vestibulum dignissim quis risus eget congue. Sed et diam libero. Phasellus non lectus sem. Cras auctor ullamcorper ullamcorper. Vestibulum dignissim quis risus eget congue. Sed et diam libero. Phasellus non lectus sem." 
    attrText = NSMutableAttributedString(string: text) 
    attrText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attrText.length)) 
    answerLabel.attributedText = attrText 

    setupConstraints() 

    contentView.frame = CGRect(x: contentView.frame.origin.x, y: contentView.frame.origin.y, width: contentView.frame.width, height: 2000) 
    scrollView.contentSize = CGSize(width: contentView.frame.width, height: 2000) 

    print(scrollView.contentSize) 
} 


func setupConstraints() { 


    //Question Contstraints 
    questionView.autoPinEdge(.top, to: .top, of: contentView, withOffset: 30) 
    questionView.autoPinEdge(.left, to: .left, of: contentView, withOffset: 0) 
    questionView.autoPinEdge(.right, to: .right, of: contentView, withOffset: 0) 

    questionView.autoPinEdge(.top, to: .top, of: questionLabel, withOffset: -10) 
    questionLabel.autoPinEdge(.right, to: .right, of: questionView, withOffset: -20) 
    questionLabel.autoPinEdge(.left, to: .left, of: questionView, withOffset: 20) 

    lineView.autoPinEdge(.top, to: .bottom, of: questionLabel, withOffset: 20) 
    lineView.autoPinEdge(.left, to: .left, of: questionView, withOffset: 20) 
    lineView.autoPinEdge(.right, to: .right, of: questionView, withOffset: -20) 
    lineView.autoSetDimensions(to: CGSize(width: lineView.frame.width, height: 2)) 

    questionContentLabel.autoPinEdge(.top, to: .bottom, of: lineView, withOffset: 20) 
    questionContentLabel.autoPinEdge(.right, to: .right, of: questionView, withOffset: -20) 
    questionContentLabel.autoPinEdge(.left, to: .left, of: questionView, withOffset: 20) 
    questionView.autoPinEdge(.bottom, to: .bottom, of: questionContentLabel, withOffset: 20) 


    //Anwser Constraints 
    answerView.autoPinEdge(.top, to: .bottom, of: questionView, withOffset: 10) 
    answerView.autoPinEdge(.left, to: .left, of: contentView, withOffset: 0) 
    answerView.autoPinEdge(.right, to: .right, of: contentView, withOffset: 0) 


    odgovorLabel.autoPinEdge(.top, to: .top, of: answerView, withOffset: 10) 
    odgovorLabel.autoPinEdge(.left, to: .left, of: answerView, withOffset: 20) 
    odgovorLabel.autoPinEdge(.right, to: .right, of: answerView, withOffset: -20) 

    answerLabel.autoPinEdge(.top, to: .bottom, of: odgovorLabel, withOffset: 20) 
    answerLabel.autoPinEdge(.left, to: .left, of: answerView, withOffset: 20) 
    answerLabel.autoPinEdge(.right, to: .right, of: answerView, withOffset: -20) 

    signLabel.autoPinEdge(.top, to: .bottom, of: answerLabel, withOffset: 20) 
    signLabel.autoPinEdge(.left, to: .left, of: answerView, withOffset: 20) 
    signLabel.autoPinEdge(.right, to: .right, of: answerView, withOffset: -20) 

    contentView.autoPinEdge(.bottom, to: .bottom, of: answerView) 

    //answerView.autoPinEdge(.bottom, to: .bottom, of: signLabel, withOffset: 20) 

} 


override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

@IBAction func backButtonPressed(_ sender: Any) { 
    navigationController?.popViewController(animated: true) 
} 
} 

: 또한 여기에 내 현재 코드입니다.

답변

0

나는 스토리 보드에서 scrollViewcontentView을 위해 설정하는 것을 제약 확실하지 않다, 나는 내가 대해 조금 궁금라고해야하는 이유 viewDidLayout에서 그들을 위해 직접 사용 설정 프레임 :

contentView.frame = CGRect(x: contentView.frame.origin.x, y: contentView.frame.origin.y, width: contentView.frame.width, height: 2000) 
scrollView.contentSize = CGSize(width: contentView.frame.width, height: 2000) 

이것을 자동으로 계산 하시겠습니까? viewController의 전체 view 포함하도록되어있는 경우

  1. 내가, 예를 들어, 계층 구조에 scrollView를 추가하고 적절하게 배치하기 위해서 자동 레이아웃 사용 : 나는 스크롤 뷰를 필요로 할 때 내가 무엇

    : 나는 수직 스크롤 scrollView을 원한다면

    scrollView.translatesAutoresizingMaskIntoConstraints = false 
    
    scrollView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true 
    scrollView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true 
    scrollView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true 
    scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true 
    
  2. 그런 다음 나는 scrollViewcontentView를 추가하고 이에 대한 적절한 레이아웃 제약을 제공해야 내가 N 제가 위에서 시작 예를 들어, 나는 다음과 같은 자동 레이아웃 제약 조건이 필요합니다 차라리 scrollView에보다 self.viewleftAnchorcontentViewrightAnchor을 제한하는 것이 여기

    contentView.translatesAutoresizingMaskIntoConstraints = false 
    
    contentView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true 
    contentView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true 
    contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true 
    contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true 
    

    사항은 고정 폭의 그것을 만들 수 있습니다. 그러나 상단 및 하단 앵커는 scrollView에 제약되어 있으므로 contentView에 더 많은 공간이 필요할 때 확장 및 스크롤 할 수 있습니다.

  3. 는 지금은 contentView에 내가 원하는 모든 콘텐츠를 추가하고, contentView 무한 높이로보기 것처럼 나는 자동 레이아웃 사용하여 배치 - scrollView 스크롤하여 전체 표현 처리됩니다. 그래서 내 예제에서 내용 만 많은 줄 하나의 거대한 UILabel 될 경우 :

    label.translatesAutoresizingMaskIntoConstraints = false 
    
    label.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true 
    label.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true 
    label.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true 
    label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true 
    

시도가 코드 가서 내가 자동 레이아웃을 사용하여 내 제약을 썼다 (제약 조건을 확인하는,하지만 난 당신이해야 추측 PureLayout과 스토리 보드에 쉽게 번역 할 수 있어야합니다.)