简体   繁体   English

无法设置translatesAutoresizingMaskIntoConstraints

[英]Can't set translatesAutoresizingMaskIntoConstraints

In an attempt to solve an Auto-Layout issue related to programmatically adding sub views to a scroll view, I have run into many references throughout the internet that, in various scenarios say to set translatesAutoresizingMaskIntoConstraints = YES or translatesAutoresizingMaskIntoConstraints = NO , depending on the case. 为了解决与以编程方式向滚动视图添加子视图相关的自动布局问题,我遇到了遍及Internet的许多引用,在各种情况下,都说要根据情况设置translatesAutoresizingMaskIntoConstraints = YEStranslatesAutoresizingMaskIntoConstraints = NO

However, in Swift, when I type: 但是,在Swift中,当我键入:

var view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false

I get the in-line error: Cannot assign to 'translatesAutoresizingMaskIntoConstraints' in 'view' . 我收到内联错误: Cannot assign to 'translatesAutoresizingMaskIntoConstraints' in 'view' Why? 为什么? Because, when inspected, you'll find that it's a parameterless function, not a property. 因为,当检查​​时,您会发现它是无参数函数,而不是属性。

I've gotten around this by subclassing, but it's a major inconvenience to have to subclass every view I'm dealing with, just to set translatesAutoresizingMaskIntoConstraints : 我已经通过子类化解决了这个问题,但是必须将我正在处理的每个视图都子类化,这是一个很大的不便,只是设置了translatesAutoresizingMaskIntoConstraints

class CardView: UIView {
    override func translatesAutoresizingMaskIntoConstraints() -> Bool {
        return false
    }
}

Does anyone know a way around this, or can shed light on the discrepancy between what the general internet councils tell you and what you can actually do, in Swift? 有谁知道解决这个问题的方法,还是可以阐明一般互联网委员会告诉您的内容与您在Swift中实际可以做什么之间的差异?

translatesAutoresizingMaskIntoConstraints is actually a method on UIView and not a property. translationsAutoresizingMaskIntoConstraints实际上是UIView上的方法,而不是属性。

The syntax works because ObjC lets you use dot-notation for calling methods as well (there's a whole other discussion on how properties actually auto-generate getter/setter methods). 语法之所以有效,是因为ObjC还允许您将点符号用于调用方法(关于属性实际上是如何自动生成getter / setter方法的讨论,还有其他讨论)。

Use the method instead of trying to use the property notation from ObjC 使用方法而不是尝试使用ObjC的属性符号

view.setTranslatesAutoresizingMaskIntoConstraints(false) 

请改用view.setTranslatesAutoresizingMaskIntoConstraints(false)

迅捷2

view.translatesAutoresizingMaskIntoConstraints = false

If you create any views in code like text views, buttons, labels, etc. You need to be careful how you add Auto Layout constraints to them. 如果您在代码中创建任何视图,例如文本视图,按钮,标签等,则需要注意如何向其添加自动布局约束。 The reason for this is that iOS creates constraints for you that match the new view's size and position, and if you try to add your own constraints these will conflict and your app will break. 原因是iOS为您创建了与新视图的大小和位置相匹配的约束,并且如果您尝试添加自己的约束,则这些约束将会冲突并且您的应用程序将中断。

Lets take an example for a uilabel: 让我们以uilabel为例:

let titleLabel = UILabel()
titleLabel.translatesAutoresizingMaskIntoConstraints = false

Likewise in your case: 同样在您的情况下:

let newView = UIView(frame: CGRectZero)
addSubview(newView)
newView.translatesAutoresizingMaskIntoConstraints = false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将autoresizingMaskIntoConstraints转换为不起作用 - translatesAutoresizingMaskIntoConstraints doesn't work 当translatesAutoresizingMaskIntoConstraints设置为NO时,视图消失。 - Views disappear when `translatesAutoresizingMaskIntoConstraints` set `NO` Xcode 4.5中在哪里设置translatesAutoresizingMaskIntoConstraints - Where to set translatesAutoresizingMaskIntoConstraints in Xcode 4.5 什么时候应该将 translatesAutoresizingMaskIntoConstraints 设置为 true? - When should translatesAutoresizingMaskIntoConstraints be set to true? 将transformsAutoresizingMaskIntoConstraints设置为false时,AutoLayout的行为会异常 - AutoLayout acts strangely when translatesAutoresizingMaskIntoConstraints set to false 当我将“translatesAutoresizingMaskIntoConstraints”设置为true,ChangeSizesInRunTime时,为什么Constrains会中断? - Why Constrains Break when I just set “translatesAutoresizingMaskIntoConstraints” to true, ChangeSizesInRunTime? 对于 ViewController 的“主”视图,您是否必须将 translatesAutoresizingMaskIntoConstraints 设置为 false? - Do you have to set translatesAutoresizingMaskIntoConstraints to false for a ViewController's 'main' view? 当我将translationsAutoresizingMaskIntoConstraints转换为false时,视图不显示 - view doesn't shows up when I turn translatesAutoresizingMaskIntoConstraints to false 将AutoresizingMaskIntoConstraints和UILabel转换为 - translatesAutoresizingMaskIntoConstraints and UILabel UiButton并将AutoresizingMaskIntoConstraints转换 - UiButton and translatesAutoresizingMaskIntoConstraints
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM