UITableViewCell AccessoryView not displaying as expected
I have a UITableviewCell with a UISwitch in an accessView and it displays correctly at runtime, but as soon as I rotate the device the switch is no longer in the right place.
I could use constraints and not use an accessory, but I thought I could use this field. Anyone have an idea?
Thank!
EDIT: here is the config in Interface Builder:
EDIT 2: I used a custom cell:
with default initialization:
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 am not setting limits at all because I am using the default properties.
source to share
It seems that if you create a code accessoryView
and editingAccessoryView
the code and install it, everything will be fine!
If you try to install them in UIStoryBoard
, it UITableViewCell
will remove constraints
them after they disappear the first time and will not add them back. It's a shame, but hopefully they change it in a later version.
Hope it helps!
source to share
I just have the same problem as you.
Here are my observations:
- If you set an accessory by code, it will always be OK whether a cell is selected or not, on both iOS 7 and iOS 8.
- If you set the AccessoryView to IB, the accessory goes to the top left corner after the first cell selection and only on iOS 8 .
I don't know if the issue is related to IB or the way the accessory is arranged in iOS 8, but so far the workaround is simple: always set the accessory in code to avoid unexpected behavior.
source to share