【问题标题】:iOS 8 Swift - ScrollViewer and Autolayout content out of screeniOS 8 Swift - ScrollViewer 和 Autolayout 内容超出屏幕
【发布时间】:2014-10-24 15:31:17
【问题描述】:

我正在尝试使用 Swift 和 Autolayout 在 iOS 上制作滚动视图,但屏幕会裁剪内容而不是调整其大小。

我的布局和约束

我该如何解决这个问题?

编辑:尝试使用 UITextView 它也不起作用

编辑 2:我正在尝试制作一个屏幕 like that,我的问题是我无法使用 AutoLayout 为所有屏幕分辨率和旋转实现此屏幕

【问题讨论】:

  • 我对你想要做的事情感到困惑。 Scrollview 的目的是裁剪它的 contentView,让你可以移动屏幕来查看整个内容。可以详细说明您要做什么。
  • 我正在尝试制作一个简单的垂直 ScrollView,如 Android ScrollView 或 XAML ScrollViewer,以及 TextWrapping 属性设置为 Wrap 的 TextBlock,所有设置为填充宽度的元素都采用屏幕尺寸,如果 TextView 大于屏幕宽度,它会增加高度以通过添加新行来显示所有文本。
  • 我的标签设置为 maxLines 0 并换行为字符换行
  • 对于您所描述的,听起来UITextView 会更好。
  • (有问题的替换图像)它也不起作用

标签: ios xcode swift autolayout


【解决方案1】:

我找到了解决办法!

仅使用 Interface Builder 添加 UIScrollView 并添加约束以适应超级视图,并以编程方式创建所有子视图。

覆盖updateViewConstraints()并为 ScrollView 集合中的每个视图 translatesAutoresizingMaskIntoConstraints 为 false 并添加宽度约束以保持滚动视图宽度。不要忘记调用 super.updateViewConstraint()。

覆盖 didRotateFromInterfaceOrientation()viewDidAppear() 并设置 scrollView.contentSize.height 以包装所有项目(如果不这样做,则无法滚动)。

class ViewController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!

    var constraintsUpdated = false;
    var label = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()

        label.numberOfLines = 0
        label.textColor = UIColor.blackColor()

        scrollView.addSubview(label)
    }

    override func updateViewConstraints() {
        if(constraintsUpdated){
            //Rotate, just update view and keep constraints
            super.updateViewConstraints()
            scrollView.layoutSubviews()
            return;
        }

        var viewsDictionary = ["label": label, "scrollView": scrollView]
        let screenWidth = self.view.frame.width

        label.setTranslatesAutoresizingMaskIntoConstraints(false)
        scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(String(format: "H:[label(==scrollView)]", screenWidth), options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary))

        constraintsUpdated = true
        super.updateViewConstraints()

        //Update subviews
        scrollView.layoutSubviews()
    }

    override func viewDidAppear(animated: Bool) {
    scrollView.contentSize.height = label.bounds.height
}

    override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
        scrollView.contentSize.height = label.bounds.height
    }

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

我找到了另一个使用 FLKAutoLayout 库的解决方案,用更少的代码就能很好地工作!

class ViewController: UIViewController {

    var labelTitle: UILabel = UILabel()

    @IBOutlet weak var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()

        //TITLE
        scrollView.addSubview(labelTitle)

        labelTitle.textColor = UIColor.blackColor()
        labelTitle.numberOfLines = 0

        //constraints
        labelTitle.constrainWidthToView(self.view, predicate: nil)
        labelTitle.numberOfLines = 0
    }

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

    override func viewDidLayoutSubviews() {
        scrollView.contentSize.height = labelTitle.frame.height
    }
}

不知道是 UIScrollView 有 AutoLayout 的 bug 还是某个特性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多