iOS - 具有scrollview内容大小的Popover大于iPhone中的popover大小

时间:2018-03-15 20:45:27

标签: ios uiscrollview uipopovercontroller popover

我是iOS开发的新手,所以它可能是一个我看不到的简单问题,问题是我在popover内部有一个scrollview,我找不到让它看起来正确的方法。

问题很可能与我试图在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 个答案:

答案 0 :(得分:0)

实际上你错过了

的一些限制

1-在nosotrosContent&滚动视图

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

nosotrosTexto& amp; nosotrosContent

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