Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 模拟器和设备中不同的自动布局_Ios_Swift_Uitableview_Autolayout_Ios Simulator - Fatal编程技术网

Ios 模拟器和设备中不同的自动布局

Ios 模拟器和设备中不同的自动布局,ios,swift,uitableview,autolayout,ios-simulator,Ios,Swift,Uitableview,Autolayout,Ios Simulator,我正在构建一个带有自动布局的UITableViewCells。它在模拟器中按预期工作,没有自动布局错误或警告 在设备上运行应用程序(与模拟器类型相同)会显示以下日志消息,布局与预期不符 2017-10-31 18:59:02.724396+0100 StaticTableTest01[8239:4373602] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of th

我正在构建一个带有自动布局的UITableViewCells。它在模拟器中按预期工作,没有自动布局错误或警告

在设备上运行应用程序(与模拟器类型相同)会显示以下日志消息,布局与预期不符

2017-10-31 18:59:02.724396+0100 StaticTableTest01[8239:4373602] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(
 "<NSLayoutConstraint:0x170099050 V:|-(10)-[UILabel:0x100e0bb90'row 0 sec: 0']   (active, names: '|':UITableViewCellContentView:0x100e06e40 )>",
 "<NSLayoutConstraint:0x170099230 UITextField:0x100f083b0.top == UILabel:0x100e0bb90'row 0 sec: 0'.top   (active)>",
 "<NSLayoutConstraint:0x170099320 UITextField:0x100f083b0.height == 41   (active)>",
 "<NSLayoutConstraint:0x1700993c0 UITextField:0x100f083b0.bottom == UITableViewCellContentView:0x100e06e40.bottom - 10   (active)>",
 "<NSLayoutConstraint:0x174093920 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x100e06e40.height == 43.5   (active)>"
)

删除在文本字段上设置高度约束的行:

let labelHeight = lb1.textRect(forBounds: CGRect(x:0, y:0, width:250, height:1000), limitedToNumberOfLines: 4).size.height
let textFieldHeight = tf1.intrinsicContentSize.height

// delete this line
//tf1.heightAnchor.constraint(equalToConstant: textFieldHeight).isActive = true

if labelHeight >= textFieldHeight
{ NSLayoutConstraint(item: lb1, attribute: .bottom, relatedBy: .equal, toItem: second, attribute: .bottom, multiplier: 1, constant: -10).isActive = true
} else
{ NSLayoutConstraint(item: tf1, attribute: .bottom, relatedBy: .equal, toItem: second, attribute: .bottom, multiplier: 1, constant: -10).isActive = true
}

由于您要求的是
.intrinsicContentSize.height
,因此尝试设置高度没有多大意义。

您的代码没有意义。在现实生活中,没有哪种情况下您会说
let cell=UITableViewCell()
。如果您在与UITableView连接时执行此操作,则表明您使用的表视图不正确。如果您在任何表视图之外执行此操作,那么就没有必要使用表视图单元格。你需要更多地思考(和解释)你真正想要做的事情。您的整个接口架构都有问题;自动布局问题只是对更大问题的诊断。您应该使用
tableView重用单元格。dequeueReusableCell(带标识符:,for:)
@mica-是否要动态创建静态单元格?这相当令人困惑。根据定义,“通过代码创建”的任何内容都不是静态的。您的图像显示了4个单元格,它们看起来都一样(除了第0行第1节有多行)。你还有别的牢房吗?或者有其他元素的细胞?您使用文本字段是因为它们是可编辑的?@mica-您确定使用了与实际设备相同的模拟器配置吗?我在iPhone7+模拟器和设备上遇到同样的约束失败,但在iPhone7上没有。不管怎样,看看我的答案。不过,给未来的提示。。。如果您需要帮助,请发布您的实际代码,或者至少发布一个演示问题的实际示例。您发布的代码不会按原样运行(
textfHeight
unresolved),并且您不会显示颜色设置或文本字段文本。因此,我们必须做一些猜测来尝试和帮助。“tableView.dequeueReusableCell不是为IB设计的单元格而设计的吗?”不,该方法是为了内存管理目的而设计的,可以智能地重用屏幕上出现/消失的单元格,而不管它们是用代码还是IB设计的。谢谢。右侧,不需要tf1.height约束。删除后,它仍然在模拟器中工作,而不是在设备上。我也没有在7+模拟器上得到约束错误隐马尔可夫模型!构建tableview时,我已将EstimateDrowEight设置为UITableViewAutomaticDimension。将该值更改为100后,模拟器和设备的行为类似,但设备上的工作台从屏幕的最上方开始(用载波、信号、电池等覆盖“状态”行),查看问题tableView?中的屏幕截图。backgroundColor=UIColor.green我认为这是因为模拟器运行iOS11和设备iOS10“…UITableViewAutomaticDimension。将其更改为100…”和“iOS11/iOS10”--这就是我在对你的问题的评论中的部分意思:如果你只显示你认为重要的代码部分,那么我们很可能遗漏了重要的部分。看这个:你是对的。有时要在最小值和完整值之间找到正确的平衡并不容易;-)谢谢你的帮助!
let labelHeight = lb1.textRect(forBounds: CGRect(x:0, y:0, width:250, height:1000), limitedToNumberOfLines: 4).size.height
let textFieldHeight = tf1.intrinsicContentSize.height

// delete this line
//tf1.heightAnchor.constraint(equalToConstant: textFieldHeight).isActive = true

if labelHeight >= textFieldHeight
{ NSLayoutConstraint(item: lb1, attribute: .bottom, relatedBy: .equal, toItem: second, attribute: .bottom, multiplier: 1, constant: -10).isActive = true
} else
{ NSLayoutConstraint(item: tf1, attribute: .bottom, relatedBy: .equal, toItem: second, attribute: .bottom, multiplier: 1, constant: -10).isActive = true
}