【发布时间】: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)
【问题讨论】:
-
请提供xcode视图层级调试前后状态截图。您可以在应用程序运行时在 XCode 中访问它:i0.wp.com/chelseatroy.com/wp-content/uploads/2020/04/…
标签: ios swift uiscrollview