programing tip

Xcode 7 iOS 9 UITableViewCell Separator Inset 문제

itbloger 2020. 11. 9. 07:54
반응형

Xcode 7 iOS 9 UITableViewCell Separator Inset 문제


이것은 질문이 아니라 내가 직면 한 문제에 대한 해결책입니다.

Xcode 7에서 애플리케이션이 iPad 장치의 iOS 9에서 실행될 때 UITableView 셀은 테이블보기의 왼쪽에 약간의 여백을 남깁니다. 그리고 장치를 가로로 회전하면 마진이 증가합니다.

내가 찾은 해결책은 다음과 같습니다.

"cellLayoutMarginsFollowReadableWidth"를 NO로 설정합니다.

self.tbl_Name.cellLayoutMarginsFollowReadableWidth = NO;

이 속성은 iOS 9에서만 사용할 수 있으므로 iOS 버전을 확인하는 조건을 설정해야합니다. 그렇지 않으면 충돌이 발생합니다.

if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_8_1)
{
    self.tbl_Name.cellLayoutMarginsFollowReadableWidth = NO;
}

다른 사람들에게 도움이되기를 바랍니다.


iOS 9 이상을위한 최상의 솔루션

이는 읽을 수있는 콘텐츠 가이드라는 새로운 기능 때문입니다. 읽기에 적합한 여백을 제공합니다. 따라서 iPhone 및 세로 iPad에서는 여백이 매우 작지만 가로 iPad에서는 더 큽니다. iOS 9에서 UITableView의 셀 여백은 기본적으로 읽을 수있는 콘텐츠 가이드를 따릅니다.

이를 중지하려면 tableView cellLayoutMarginsFollowReadableWidthNO/false.

출처 : https://forums.developer.apple.com/thread/5496


iOS 9까지 완벽한 솔루션

viewDidLoad에서

목표 -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 메서드에서 다음 코드를 추가합니다.

목표 -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's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    // Explictly set your cell's 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's margin settings
    if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
      cell.preservesSuperviewLayoutMargins = false
    }

    // Explictly set your cell's layout margins
    if cell.respondsToSelector(Selector("setLayoutMargins:")) {
      cell.layoutMargins = UIEdgeInsetsZero
    }
  }

조금 늦었습니다. 다른 사람에게 도움이 되었으면합니다 ...

if #available(iOS 9.0, *) {
      myTableView.cellLayoutMarginsFollowReadableWidth = false
}

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

Note: In the code above the bottom and top anchors use the contentView instead of the readableContentGuide so that the content vertical margins changes based on the tableView.rowHeight.

참고URL : https://stackoverflow.com/questions/31536187/xcode-7-ios-9-uitableviewcell-separator-inset-issue

반응형