【问题标题】:Why does scrollview still scroll even when it has no contentView to scroll into为什么滚动视图即使没有 contentView 滚动到仍然滚动
【发布时间】:2015-11-30 05:04:53
【问题描述】:

谁能解释为什么橙色方块不在 0 点,而我仍然可以将滚动视图上下滚动 20 像素?

导入 UIKit

类 MessageVC: UIViewController, UIScrollViewDelegate {

let scrollView = UIScrollView()


let messageHolder = MessageHolderVC()
let messageSender = MessageSenderVC()

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.whiteColor()


    scrollView.frame = CGRectMake(0, 1, self.view.frame.width, self.view.frame.height - 2)
    scrollView.backgroundColor = UIColor.redColor()
    scrollView.pagingEnabled = true

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 2, scrollView.frame.size.height)
    scrollView.bounces = false

    self.view.addSubview(scrollView)


    messageHolder.view.frame = CGRectMake(0, 0, 20, 20)
    scrollView.addSubview(messageHolder.view)




}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

【问题讨论】:

    标签: ios swift uiscrollview


    【解决方案1】:

    试试这个:在你的故事板中选择ViewController并取消选中Adjust Scroll View insets,像这样:

    【讨论】:

    • 抱歉,完全是在代码中完成的。我可以在代码中做到这一点。它从来都不是问题,所以我猜这是新的东西。
    • 然后试试这个:self.automaticallyAdjustsScrollViewInsets = false
    • 那行得通。谢谢 Q 先生。请您指点我的方向,让我对此有更多的了解吗?
    • here's 一些关于滚动视图内容插图的问答。
    • 在 iOS 7 中,Apple 引入了半透明导航栏,以便 scrollView 的实际框架位于顶部,内容将从 statusBarHeight + navBarHeight 位置开始,因此当向上滚动内容时位于导航栏后面以创建该效果。在您的情况下,您没有导航栏,这就是为什么只添加 statusBarHeight
    猜你喜欢
    • 1970-01-01
    • 2020-04-03
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多