UIScrollView 未显示在 UINavigationItem 中
UIScrollView not displaying in UINavigationItem
在我的 UIView 子类中,我添加了以下 init 方法,然后我将此视图指定为 navigationItem 上的 titleView。我看到一个红色的矩形,这意味着这个视图在那里,但我没有看到黄色的滚动视图。我在这里做错了什么?
open class ChatNavBarImagesView : UIView {
private let scrollView = UIScrollView()
public init(navigationController: UINavigationController) {
let height = navigationController.navigationBar.frame.height
super.init(frame: CGRect(x: 0, y: 0, width: 200, height: height))
backgroundColor = UIColor.red
addSubview(scrollView)
scrollView.leadingAnchor.constraint(equalTo: leadingAnchor)
scrollView.trailingAnchor.constraint(equalTo: trailingAnchor)
scrollView.topAnchor.constraint(equalTo: topAnchor)
scrollView.bottomAnchor.constraint(equalTo: bottomAnchor)
scrollView.backgroundColor = UIColor.yellow
scrollView.contentSize = CGSize(width: 200, height: height)
}
您需要将 scrollView 的框架设置为 ChatNavBarImagesView 的边界
scrollView.frame = bounds
addSubview(scrollView)
在我的 UIView 子类中,我添加了以下 init 方法,然后我将此视图指定为 navigationItem 上的 titleView。我看到一个红色的矩形,这意味着这个视图在那里,但我没有看到黄色的滚动视图。我在这里做错了什么?
open class ChatNavBarImagesView : UIView {
private let scrollView = UIScrollView()
public init(navigationController: UINavigationController) {
let height = navigationController.navigationBar.frame.height
super.init(frame: CGRect(x: 0, y: 0, width: 200, height: height))
backgroundColor = UIColor.red
addSubview(scrollView)
scrollView.leadingAnchor.constraint(equalTo: leadingAnchor)
scrollView.trailingAnchor.constraint(equalTo: trailingAnchor)
scrollView.topAnchor.constraint(equalTo: topAnchor)
scrollView.bottomAnchor.constraint(equalTo: bottomAnchor)
scrollView.backgroundColor = UIColor.yellow
scrollView.contentSize = CGSize(width: 200, height: height)
}
您需要将 scrollView 的框架设置为 ChatNavBarImagesView 的边界
scrollView.frame = bounds
addSubview(scrollView)