只有最后添加的视图在UIScrollView iOS中可见



>我遇到了滚动视图的问题。我在故事板中添加了滚动视图

视图 - 主视图 - 滚动视图 - 帐户保存子视图

现在我正在尝试在帐户保存子视图中添加视图,如下所示

    //MARK:- Create and add wallets on the UI
    private func createAndAddWallets()
    {
        var yPosition : CGFloat = 0.0
        //1. Create left hand side wallets with help of keys
        for key in dictData.keys
        {
            let walletView:Wallet = Bundle.main.loadNibNamed("Wallet", owner: self, options: nil)?.first as! Wallet
            walletView.translatesAutoresizingMaskIntoConstraints = false
            walletView.frame = CGRect(x: 0.0, y: yPosition, width: accountSavingSubView.frame.width, height: accountSavingSubView.frame.height)
            walletView.accountNameLabel.text = key
            accountSavingSubView.addSubview(walletView)
            yPosition += walletView.frame.maxY
            walletView.isUserInteractionEnabled = true
            print("walletView frame is :(walletView.frame)")
        }
        //set continer scroll content size
            self.contanerScrollView.contentSize = CGSize(width: self.accountSavingSubView.frame.width, height: CGFloat(80.0/320.0)*UIScreen.main.bounds.width*CGFloat(self.dictData.keys.count))
}

其中 dictData 是 ([String: String]) 的字典。

但在我看来,我只能看到添加了最后一个视图(x=0,y=0)。ScrollView内容大小是正确的,我可以滚动而没有视图,除了最后一个在第一位(x:0,y:0)。

请在添加视图时让我知道我在代码中做错了什么。

你需要对此发表评论

walletView.translatesAutoresizingMaskIntoConstraints = false

因为它仅用于设置约束,并验证此处提供的帧值不是 0

walletView.frame = CGRect(x: 0.0, y: yPosition, width: accountSavingSubView.frame.width, height: accountSavingSubView.frame.height)

顺便说一句,您也可以对其进行注释,因为如果您将其设置为自由大小,则在XIB中设置帧时将捕获该帧

最新更新