iOS - 滚动视图内容大小的弹出窗口大于 iPhone 中的弹出窗口大小

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

我是 iOS 开发的新手,所以这可能是一个我看不到的简单问题,问题是我在弹出窗口中有一个滚动视图,但我找不到制作方法看起来不错。

这个问题很可能与我试图在 iPhone 中使用非全屏弹出窗口这一事实有关。在这种特殊情况下,可以通过更改它来解决,但如果可能的话,我想知道该怎么做。

此外,它只能横向滚动,我希望它只能在垂直轴上滚动。 (我还没有研究这个,所以它可能真的很简单而且并不重要)

这是问题的图片:

Image of how the view doesnt fit in the pop over

弹出窗口左侧有文本,右侧有图像继续

这是我的代码

@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