2017-09-14 220 views
0

我在調整大小時遇到​​了一些問題,並且在將內容添加到我的標籤時使scrollview垂直滾動。我的層次結構是這樣的:根據內容調整uiscrollview的大小

my hierarchy

雖然我的看法是這樣的:

my view

所以我想填寫的內容標籤,在我的代碼在啓動時,我得到的文本來自API調用。但出於測試目的,我只是對一些lorem ipsum文本進行了硬編碼,以使其工作。所以我的內容被添加到標籤和UILabels調整大小,我的意見,包含標籤調整大小。我有問題調整我的scrollview內容,以便我可以滾動如果標籤中的文本更長。另外這裏是我當前的代碼:

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.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. 然後,我需要一個contentView添加到scrollView併爲其提供適當的佈局限制,所以如果我想垂直滾動scrollView我n個I上面開始的實例中,我需要以下自動佈局約束:這裏

    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 
    

    注意,我制約leftAnchorcontentViewrightAnchorself.view而非scrollView,使其固定的寬度。但是,頂部和底部的錨點限制爲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和故事板)。

相關問題