Xcode 7 iOS 9 UITableViewCell Separator Вставка
Это не вопрос, а решение проблемы, с которой я столкнулся.
В Xcode 7, когда приложение запускается на iOS 9 на устройствах iPad, ячейки UITableView оставляют некоторый запас в левой части таблицы. И поворот устройства в ландшафт увеличит поля.
Я нашел решение:
Настройка "cellLayoutMarginsFollowReadableWidth" на NO.
self.tbl_Name.cellLayoutMarginsFollowReadableWidth = NO;
Так как это свойство доступно только в iOS 9. Итак, вам нужно будет установить условие для проверки версии iOS, иначе оно будет аварийно.
if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_8_1)
{
self.tbl_Name.cellLayoutMarginsFollowReadableWidth = NO;
}
Надеюсь, что это полезно другим.
Ответы
Ответ 1
Лучшее решение для iOS 9 и выше
Это связано с новой функцией, называемой читаемыми руководствами по содержанию. Он обеспечивает поля, подходящие для чтения. Итак, в iPhone и портрете iPad они очень маленькие, но в плане iPad они больше. В iOS 9 поля поля UITableView по умолчанию соответствуют следующему читаемому руководству по содержанию.
Если вы хотите остановить это, просто установите tableView cellLayoutMarginsFollowReadableWidth
на NO/false
.
Источник: https://forums.developer.apple.com/thread/5496
Ответ 2
Идеальное решение до iOS 9
В представленииDidLoad
Objective-C
- (void)viewDidLoad {
[super viewDidLoad];
//Required for iOS 9
if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0) {
self.testTableView.cellLayoutMarginsFollowReadableWidth = NO;
}
}
Свифта
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 9.0, *) {
tableViewDiet.cellLayoutMarginsFollowReadableWidth = false
}
}
В методах TableViewDelegate добавьте следующий код:
Objective-C
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
// Prevent the cell from inheriting the Table View margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
// Explictly set your cell layout margins
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
Свифта
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
// Remove seperator inset
if cell.respondsToSelector(Selector("setSeparatorInset:")) {
cell.separatorInset = UIEdgeInsetsZero
}
// Prevent the cell from inheriting the Table View margin settings
if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
cell.preservesSuperviewLayoutMargins = false
}
// Explictly set your cell layout margins
if cell.respondsToSelector(Selector("setLayoutMargins:")) {
cell.layoutMargins = UIEdgeInsetsZero
}
}
Ответ 3
Немного поздно. Надеюсь, это полезно для кого-то еще...
if #available(iOS 9.0, *) {
myTableView.cellLayoutMarginsFollowReadableWidth = false
}
Ответ 4
readableContentGuide
- это руководство по компоновке, которое уже добавлено к каждому UIView
Это означает, что пользователю не нужно поворачивать голову, чтобы прочитать содержимое.
Если вы хотите следовать читабельному руководству по содержанию, выполните следующие действия:
let baseSection = UIView()
contentView.addSubview(baseSection)
baseSection.translatesAutoresizingMaskIntoConstraints = false
let insets = UIEdgeInsets(top: 4, left: 0, bottom: 4, right: 0)
baseSection.leadingAnchor.constraint(equalTo: readableContentGuide.leadingAnchor, constant: insets.left).isActive = true
baseSection.trailingAnchor.constraint(equalTo: readableContentGuide.trailingAnchor, constant: -insets.right).isActive = true
baseSection.topAnchor.constraint(equalTo: contentView.topAnchor, constant: insets.top).isActive = true
baseSection.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -insets.bottom).isActive = true
Примечание. В приведенном выше коде нижний и верхний анкеры используют contentView вместо readableContentGuide
, чтобы изменения по вертикали содержимого изменялись на основе tableView.rowHeight
.