Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIScrollView不会再次出现_Ios_Swift_Uiscrollview - Fatal编程技术网

Ios UIScrollView不会再次出现

Ios UIScrollView不会再次出现,ios,swift,uiscrollview,Ios,Swift,Uiscrollview,我正在使用SpriteKit构建一个应用程序,因此我只使用一个ViewController来添加或删除子视图。我总是添加一个子视图的新实例 当我试图添加一个UIScrollView时,它在我第一次添加它时显示得非常好。 但是,在我删除UIScrollView并再次添加它之后(一个UIScrollView的新实例)。UIScrollView不会显示 UIScrollView和UIStackView内部的框架第一次和第二次是相同的 我不太明白它为什么不能正常工作。我猜这与自动布局有关,但同样,第一次

我正在使用SpriteKit构建一个应用程序,因此我只使用一个ViewController来添加或删除子视图。我总是添加一个子视图的新实例

当我试图添加一个
UIScrollView
时,它在我第一次添加它时显示得非常好。 但是,在我删除
UIScrollView
并再次添加它之后(一个
UIScrollView
的新实例)。
UIScrollView
不会显示

UIScrollView
UIStackView
内部的框架第一次和第二次是相同的

我不太明白它为什么不能正常工作。我猜这与自动布局有关,但同样,第一次和第二次添加帧时,帧是相同的。 而且,我不想在这里实现自动布局

下面是课堂:

class StoreScrollV: UIScrollView {
    override init(frame: CGRect) {
        super.init(frame: rectOfEntireScreen)
        self.bounds.size = CGSize(width: 300, height: 300)
        self.contentSize = CGSize(width: 1000, height: 300)
        self.tag = 100
        
        let stackView = UIStackView()
        self.addSubview(stackView)
        stackView.tag = 111
        stackView.frame.size = CGSize(width: 1000, height: 300)
        stackView.frame = stackView.toCenter() 
        //custome function that move the view to the center of its parent with the same size.

        stackView.axis = .horizontal

        stackView.translatesAutoresizingMaskIntoConstraints = false
        self.translatesAutoresizingMaskIntoConstraints = false
        
        let imageV1 = UIImageView(image: UIImage(named: "ballCat"))
        stackView.addArrangedSubview(imageV1)
        
        let imageV2 = UIImageView(image: UIImage(named: "ballChicken"))
        stackView.addArrangedSubview(imageV2)
     
        stackView.spacing = 10;
        stackView.distribution = .equalCentering
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func willMove(toWindow newWindow: UIWindow?) {
        if newWindow != nil {
//            joinAnimationFromTop(view: self)
        } else {
//            leaveAnimationResetToTop(view: self)
        }
    } 
}
下面是我如何添加它的: (
UIScrollView
位于另一个添加的
UIView
中)

改为

        stackView.translatesAutoresizingMaskIntoConstraints = true
        self.translatesAutoresizingMaskIntoConstraints = true
问题解决了

scrollView的帧实际上是(0,0,0,0)。我没有意识到这一点,因为在调试时,我在init函数的末尾打印出了scrollView的框架

因此,框架是正确的。当我使用Kamil建议的视图层次调试时,我意识到框架总是错误的


但是,我仍然不明白,如果帧是(0,0,0,0),为什么它会第一次出现。

请提供xcode视图层次结构调试前后的屏幕截图。当应用程序在此处运行时,您可以在XCode中访问它:
        stackView.translatesAutoresizingMaskIntoConstraints = false
        self.translatesAutoresizingMaskIntoConstraints = false
        stackView.translatesAutoresizingMaskIntoConstraints = true
        self.translatesAutoresizingMaskIntoConstraints = true