简体   繁体   English

UITableViewCell AccessoryView 显示不正确

[英]UITableViewCell AccessoryView not displayed properly

I have a UITableviewCell with a UISwitch in the accessoryView and it is displayed properly at runtime, but as soon as I rotate the device, the switch isn't anymore at the right place.我在附件视图中有一个带有 UISwitch 的 UITableviewCell 并且它在运行时正确显示,但是一旦我旋转设备,开关就不再位于正确的位置。附件视图的错误位置

I could use constraints and not use the accessoryView, but I thought I could use this field.. Does anyone have an idea ?我可以使用约束而不是使用附件视图,但我想我可以使用这个字段.. 有没有人有想法?

Thanks !谢谢 !

EDIT : here is the configuration in Interface Builder :编辑:这是 Interface Builder 中的配置:在此处输入图片说明

EDIT 2 : I used a Custom cell :编辑 2:我使用了自定义单元格:在此处输入图片说明

with initialization on the default properties :对默认属性进行初始化:

self.textLabel.font = [UIFont preferredDynamicFontForTextStyle:UIFontTextStyleBody fontSize:16.0];
self.backgroundColor = self.contentView.backgroundColor;

self.textLabel.text = item.title;
self.selectedImage = item.imageSelected;
self.unselectedImage = item.image;
self.imageView.image = self.unselectedImage;
// if I do it programmatically
self.accessoryView = [[UISwitch alloc] init];

I don't set constraints at all, because I use the default properties..我根本不设置约束,因为我使用默认属性..

I'm just having the same issue as you. 我和你有同样的问题。

Here are my observations: 以下是我的观察:

  • If you set the accessoryView by code, it's always OK, whether the cell is selected or not, on both iOS 7 and iOS 8. 如果您按代码设置accessoryView,则无论是否选择了单元格,iOS 7和iOS 8都可以。
  • If you set the accessoryView by IB, the accessoryView moves to the top left corner after the first selection of the cell, and only on iOS 8 . 如果您通过IB设置accessoryView,则在第一次选择单元格后 ,accessoryView将移至左上角,并且仅在iOS 8上移动

I don't know if the issue is from IB or the way cells layout the accessory view on iOS 8 but so far the workaround is simple: always set the accessoryView by code to avoid unexpected behavior. 我不知道问题是来自IB还是单元格在iOS 8上布局附件视图的方式,但到目前为止,解决方法很简单:始终按代码设置accessoryView以避免意外行为。

I can say with some certainty that your constraints are not properly set up. 我可以肯定地说你的约束没有正确设置。 But without seeing the storyboard's constraints, there's no way to say what is broken for sure. 但是,如果没有看到故事板的限制,就没有办法说出什么是肯定的。

如果您正在使用autolayout(可能),则应首先设置约束

Looks like if you create the accessoryView and the editingAccessoryView in code and set it, everything works great! 看起来如果你在代码中创建accessoryVieweditingAccessoryView并设置它,一切都很棒!

If you try to set them in the UIStoryBoard , the UITableViewCell will remove the constraints after they fade away the first time, and it won't add them back. 如果您尝试在UIStoryBoard设置它们, UITableViewCell将在它们第一次消失后删除constraints ,并且它不会将它们添加回来。 It's a shame, but hopefully they change it in a later version. 这是一种耻辱,但希望他们在以后的版本中改变它。

Hope that Helps! 希望有帮助!

For anyone finding this issue currently, I think I have an explanation of what might be happening here:对于目前发现这个问题的任何人,我想我对这里可能发生的事情有一个解释:

This is an issue with the translatesAutoresizingMaskIntoConstraints property on the view.这是视图上 translatesAutoresizingMaskIntoConstraints 属性的问题。 From Apple's documentation, particularly relevant bits in bold:来自 Apple 的文档,特别是相关的粗体部分:

If this property's value is YES, the system creates a set of constraints that duplicate the behavior specified by the view's autoresizing mask.如果此属性的值为 YES,则系统会创建一组约束来复制由视图的自动调整大小掩码指定的行为。 This also lets you modify the view's size and location using the view's frame, bounds, or center properties, allowing you to create a static, frame-based layout within Auto Layout.这还允许您使用视图的框架、边界或中心属性修改视图的大小和位置,从而允许您在自动布局中创建静态的、基于框架的布局。 Note that the autoresizing mask constraints fully specify the view's size and position;请注意,自动调整大小蒙版约束完全指定了视图的大小和位置; therefore, you cannot add additional constraints to modify this size or position without introducing conflicts.因此,您不能在不引入冲突的情况下添加额外的约束来修改此大小或位置。 If you want to use Auto Layout to dynamically calculate the size and position of your view, you must set this property to NO, and then provide a non ambiguous, nonconflicting set of constraints for the view.如果要使用自动布局来动态计算视图的大小和位置,则必须将此属性设置为 NO,然后为视图提供一组明确、无冲突的约束。 By default, the property is set to YES for any view you programmatically create.默认情况下,对于您以编程方式创建的任何视图,该属性都设置为 YES。 If you add views in Interface Builder, the system automatically sets this property to NO .如果在 Interface Builder 中添加视图,系统会自动将此属性设置为 NO

So, when I came across this issue it was when I was changing the tableview from the contents being a stack view with label and switch into being a label with a UISwitch as the accessory view.因此,当我遇到这个问题时,是在我将 tableview 从内容更改为带有标签的堆栈视图并切换为带有 UISwitch 作为附件视图的标签时。 The switch was being programmatically created earlier on view load for the container view the table was sitting in and the translatesAutoresizingMaskIntoConstraints was being set to NO - essentially the same situation that happens when you create the switch in interface builder.该开关是在视图加载时为表所在的容器视图以编程方式创建的,并且 translatesAutoresizingMaskIntoConstraints 被设置为 NO - 本质上与您在界面构建器中创建开关时发生的情况相同。 The code I was changing then tried to set up constraints but the switch would always bug out and be floating over to the wrong side when you rotated the screen.我正在更改的代码然后尝试设置约束,但是当您旋转屏幕时,开关总是会出错并浮动到错误的一侧。 The solution for me was changing the translatesAutoresizingMaskIntoConstraints property to YES and letting the constraints be created by the view itself.我的解决方案是将 translatesAutoresizingMaskIntoConstraints 属性更改为 YES 并让约束由视图本身创建。 It looks to me like this is a conflicting issue between what this property does on most views and what the accessory view itself tries to handle and if a programmer tries to go in and set things up themselves there seems to not be a good indication of what has gone wrong here.在我看来,这是此属性在大多数视图上的作用与附件视图本身试图处理的内容之间存在冲突的问题,如果程序员试图进入并自行设置,似乎无法很好地说明什么这里出错了。 Hope this helps someone in the future!希望这对未来的人有所帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM