iOS:自动布局的问题。模拟器结果更改为故事板更改

问题描述 投票:0回答:2

我正在制作我的第一个iOS应用程序。我遇到了自动布局和滚动视图的问题。所以现在我在主故事板上有一个滚动视图。滚动视图应占据整个屏幕。我有其他三个xib文件。每个都是滚动视图中的页面。所以我可以实现类似滚动式演练的东西。我现在面临的问题是。如果我改变主要的故事板大小,我从模拟器得到的结果将是不同的。

此外,当应用程序加载时,它不会立即显示我在屏幕上的东西。它只会显示纯黑色背景。我需要先在屏幕上滚动,然后显示所有文字,图像。我也不确定是什么问题。我没有主要故事板的背景颜色。这可能与上述问题有关吗?


第一个问题的一些例子,如果我将主要故事板改为iPhone Plus尺寸。模拟器将显示iPhone Plus设备的正确尺寸,但不适用于普通iPhone和iPhone X.如下图所示。

enter image description here

如果我将故事板更改为常规iPhone大小。 iPhone 8将显示正确的尺寸,但加号和X将关闭。

enter image description here

这就是我为滚动视图设置约束的方法。

enter image description here

这就是我在代码中设置单个xib和滚动视图的方法。

override func viewDidLoad() {
    super.viewDidLoad()

    //Set up individual view for scroll view

    let xOrigin = self.view.frame.width

    creditScreen = CreditScreenView(nibName: "CreditScreenView", bundle: nil)

    mainScroll.addSubview(creditScreen.view)

    forWeather = ForcastScreenView(nibName: "ForcastScreenView", bundle: nil)

    mainScroll.addSubview(forWeather.view)

    nowWeather  = NowScreenView(nibName: "NowScreenView", bundle: nil)

    mainScroll.addSubview(nowWeather.view)

    //mainScroll.translatesAutoresizingMaskIntoConstraints = true

    // set up the size for each view
    creditScreen.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

    forWeather.view.frame = CGRect(x: xOrigin, y: 0, width: self.view.frame.width, height: self.view.frame.height)

    nowWeather.view.frame = CGRect(x: xOrigin * 2, y: 0, width: self.view.frame.width, height: self.view.frame.height)

    mainScroll.contentSize = CGSize(width: CGFloat(view.frame.width * 3), height: CGFloat(view.frame.height))
    mainScroll.contentOffset.x = view.frame.width * 3

    mainScroll.setContentOffset(CGPoint(x: view.frame.width * 2, y: 0), animated: true)

    //Set up the location manager here.
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()


}

我不确定导致问题的原因,也许是我如何设置xib?当我搜索这个问题时,有人建议将主视图中的视图作为顶级视图,然后将滚动视图放在该顶级视图中。它对我不起作用。有人可以帮忙解决这个问题吗?谢谢!

如果需要,这是源代码的GitHub。 https://github.com/lucky13820/air

ios swift xcode uiscrollview autolayout
2个回答
1
投票

要回答黑屏问题,请使用此代码

mainScroll.setContentOffset(CGPoint(x:view.frame.width * 2,y:0),animated:true)

以正确的比例将它向右移动,我已经在iPhone 8,8 +和X中进行了测试。


1
投票

经过更多的研究和大量的试验和错误,我修复了它。

我添加了两行代码并关闭属性检查器中scrollview的autoresize子视图。

mainScroll.contentSize = CGSize(width: CGFloat(xOrigin * 3), height: CGFloat(self.view.frame.height))

mainScroll.translatesAutoresizingMaskIntoConstraints = true

谢谢大家。

© www.soinside.com 2019 - 2024. All rights reserved.