簡體   English   中英

iOS-具有滾動視圖內容大小的Popover大於iPhone中的Popover大小

[英]iOS - Popover with scrollview content size is bigger than the popover size in iPhone

我是iOS開發的新手,所以這可能是一個我看不到的簡單問題,問題是我在彈出窗口中有一個滾動視圖,而我找不到使它看起來正確的方法。

該問題很可能與以下事實有關:我正在嘗試在iPhone中使用非全屏彈出窗口。 在這種特殊情況下,可以解決此問題,但是如果可能的話,我想知道如何去做。

而且它只能橫向滾動,我希望它只能沿垂直軸滾動。 (我尚未對此進行研究,因此它可能真的很簡單,並不重要)

這是問題的圖像:

視圖如何不適合彈出窗口的圖像

彈出框左側有文本,圖像繼續右側有文本

這是我的代碼

@objc func foo(_ sender: UITapGestureRecognizer) {

    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let popupVC = storyboard.instantiateViewController(withIdentifier: "popup")

    popupVC.modalTransitionStyle = .crossDissolve
    popupVC.modalPresentationStyle = .popover
    popupVC.preferredContentSize = CGSize(width: view.bounds.width * 0.75, height: view.bounds.height * 0.75)

    let pVC = popupVC.popoverPresentationController
    pVC?.permittedArrowDirections = .any
    pVC?.delegate = self
    pVC?.sourceView = sender.view!
    pVC?.sourceRect = sender.view!.bounds

    let popView = popupVC.view!

    let nosotrosFoto = UIImageView()
    nosotrosFoto.image = UIImage(named: "foto.png")
    nosotrosFoto.contentMode = UIViewContentMode.scaleAspectFit
    nosotrosFoto.translatesAutoresizingMaskIntoConstraints = false

    let nosotrosTexto = UILabel()
    nosotrosTexto.text = sobreNosotrosString
    nosotrosTexto.translatesAutoresizingMaskIntoConstraints = false
    nosotrosTexto.numberOfLines = 0

    let nosotrosContent = UIView()
    nosotrosContent.translatesAutoresizingMaskIntoConstraints = false
    nosotrosContent.contentMode = UIViewContentMode.scaleToFill
    nosotrosContent.addSubview(nosotrosTexto)
    nosotrosContent.addSubview(nosotrosFoto)

    nosotrosFoto.topAnchor.constraint(equalTo: nosotrosContent.topAnchor).isActive = true
    nosotrosFoto.leftAnchor.constraint(equalTo: nosotrosContent.leftAnchor).isActive = true
    nosotrosFoto.rightAnchor.constraint(equalTo: nosotrosContent.rightAnchor).isActive = true

    nosotrosTexto.topAnchor.constraint(equalTo: nosotrosFoto.bottomAnchor).isActive = true
    nosotrosTexto.leftAnchor.constraint(equalTo: nosotrosFoto.leftAnchor).isActive = true
    nosotrosTexto.rightAnchor.constraint(equalTo: nosotrosFoto.rightAnchor).isActive = true

    let nosotrosScroll = UIScrollView(frame: popView.bounds)
    nosotrosScroll.contentSize =  popupVC.preferredContentSize
    nosotrosScroll.translatesAutoresizingMaskIntoConstraints = false
    nosotrosScroll.contentMode = UIViewContentMode.scaleAspectFit
    nosotrosScroll.showsVerticalScrollIndicator = true
    nosotrosScroll.backgroundColor = UIColor.blue
    nosotrosScroll.addSubview(nosotrosContent)

    popView.addSubview(nosotrosScroll)

    nosotrosScroll.topAnchor.constraint(equalTo: popView.layoutMarginsGuide.topAnchor).isActive = true
    nosotrosScroll.leftAnchor.constraint(equalTo: popView.layoutMarginsGuide.leftAnchor).isActive = true
    nosotrosScroll.rightAnchor.constraint(equalTo: popView.layoutMarginsGuide.rightAnchor).isActive = true
    nosotrosScroll.bottomAnchor.constraint(equalTo: popView.layoutMarginsGuide.bottomAnchor).isActive = true

    self.present(popupVC, animated: true, completion: nil)
}

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.none
}

實際上,您錯過了一些限制

1-在nosotrosContent和scrollView之間

nosotrosContent.topAnchor.constraint(equalTo: nosotrosScroll.layoutMarginsGuide.topAnchor).isActive = true
nosotrosContent.leftAnchor.constraint(equalTo: nosotrosScroll.layoutMarginsGuide.leftAnchor).isActive = true
nosotrosContent.rightAnchor.constraint(equalTo: nosotrosScroll.layoutMarginsGuide.rightAnchor).isActive = true
nosotrosContent.bottomAnchor.constraint(equalTo: nosotrosScroll.layoutMarginsGuide.bottomAnchor).isActive = true
nosotrosContent.widthAnchor.constraint(equalTo: popView.widthAnchor).isActive = true

2-照片的高度,說100

nosotrosFoto.heightAnchor.constraint(equalToConstant: 100.0).isActive = true

3- nosotrosTexto和nosotrosContent之間的底部約束

nosotrosTexto.bottomAnchor.constraint(equalTo: nosotrosTexto.bottomAnchor , constant: -20 ).isActive = true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM