【问题标题】:UIScrollView Does not show up the second time aroundUIScrollView 第二次不显示
【发布时间】:2020-12-31 12:05:51
【问题描述】:

我正在使用 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 中)

let storePage = StoreView() //another customized UIView frame is the entire screen at 0,0.
let scrV = StoreScrollV()

storePage.addSubview(scrV)
scrV.frame = scrV.toCenter()
//custome function that move the view to the center of its parent with the same size.

VC.addSubview(viewWithScrollV)

hierarchy debugging the second time ScrollView is added

hierarchy debugging the second time ScrollView is added

【问题讨论】:

标签: ios swift uiscrollview


【解决方案1】:
        stackView.translatesAutoresizingMaskIntoConstraints = false
        self.translatesAutoresizingMaskIntoConstraints = false

改成

        stackView.translatesAutoresizingMaskIntoConstraints = true
        self.translatesAutoresizingMaskIntoConstraints = true

问题已解决。

scrollView 的 frame 实际上是 (0,0,0,0)。我没有意识到这一点,因为我在调试的时候,在init函数的最后打印出了scrollView的frame。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    • 2017-04-03
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 2017-08-10
    相关资源
    最近更新 更多