2017-09-14 16 views
0

内容がラベルに追加されると、サイズを変更してスクロールビューを垂直方向にスクロールできる問題があります。私の階層は次のようになります。内容に基づいてuiscrollviewのサイズを変更します

my hierarchy

私の見解は次のように見えますが:

my view

だから私は、私は、テキストを取得するときに起動時に私のコードでコンテンツとラベルを記入したいと思いますAPI呼び出しから取得します。しかしテスト目的のために、私はそれを動作させるためにいくつかのlorem ipsumテキストをハードコーディングしました。したがって、私のコンテンツはラベルに追加され、UILabelsのサイズが変更され、ラベルを含むビューもサイズ変更されます。スクロールビューのコンテンツのサイズを変更すると問題が発生し、ラベル内のテキストが長い場合はスクロールできます。また、ここでは私の現在のコードは次のとおりです。

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

それは私がテキストの長さに基づいて、私のラベルやビューのサイズ変更を行うことができた唯一の方法だったので、私もPureLayoutを使用しています。

答えて

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) 

これは自動的に計算されますか?

  1. 私は階層にscrollViewを追加し、viewControllerの全体viewをカバーすることになっている場合には、例えば、それを適切にレイアウトする自動レイアウトを使用します。私は、スクロール可能なビューを必要なときに何をすべきか

    :私は、垂直スクロール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は、私は上記の始めの例では、私は次の自動レイアウトの制約が必要になります。私は、固定幅のそれを作るためにself.viewにではなく、scrollViewcontentViewleftAnchorrightAnchorを制約ここ

    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とストーリーボードの両方に簡単に翻訳することができます)。

関連する問題