2015-07-21 36 views
67

Đây không phải là một câu hỏi, đúng hơn là một giải pháp cho vấn đề tôi gặp phải.Xcode 7 iOS 9 UITableViewCell Separator Inset issue

Trong Xcode 7, khi ứng dụng được chạy trên iOS 9 trên thiết bị iPad, các ô UITableView để lại một số lề ở phía bên trái của chế độ xem bảng. Và xoay thiết bị sang cảnh quan sẽ làm tăng biên lợi nhuận.

Giải pháp tôi tìm thấy là:

Đặt "cellLayoutMarginsFollowReadableWidth" to NO.

self.tbl_Name.cellLayoutMarginsFollowReadableWidth = NO; 

Vì, thuộc tính này chỉ có sẵn trong iOS 9. Vì vậy, bạn sẽ phải đặt điều kiện để kiểm tra phiên bản iOS, nếu không nó sẽ bị lỗi.

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

Hy vọng nó hữu ích cho người khác.

+0

[Có cùng một vấn đề] (http://stackoverflow.com/questions/31537196/ios-9-uitableview-separators-significant-left-margin) –

+0

Có, điều này làm việc hoàn hảo cho tôi đặc biệt là trên iPad với iOS 9 –

+0

Tôi vừa gặp phải vấn đề nghiêm trọng với câu trả lời này, nó sử dụng phiên bản 'NSFoundationVersionNumber_iOS_8_1', nó cần sử dụng' NSFoundationVersionNumber_iOS_8_4' thay vào đó, nếu không nó sẽ làm hỏng ứng dụng chạy iOS8.2 và iOS8.3 và IOS8.4 – Alex

Trả lời

13

Giải pháp hoàn hảo tối đa iOS 9

Trong viewDidLoad

Objective-C

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    //Required for iOS 9 
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0) { 
     self.testTableView.cellLayoutMarginsFollowReadableWidth = NO; 
    } 
} 

Swift

override func viewDidLoad() { 
    super.viewDidLoad() 
    if #available(iOS 9.0, *) { 
     tableViewDiet.cellLayoutMarginsFollowReadableWidth = false 
    } 
    } 

Trong phương pháp TableViewDelegate add đoạn code sau đây:

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'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]; 
    } 
} 

Swift

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 
    } 
    } 
+1

Điều này không hiệu quả đối với tôi. Đó là lý do tại sao tôi phải tìm cách tiếp cận khác. – stuti

+1

Tính năng này không hoạt động với iOS 9. Bạn đã thử nghiệm nó với iOS 9 và Xcode 7 với các công cụ dòng lệnh được đặt đúng trong tùy chọn chưa? –

+0

Không. Điều này không hiệu quả với tôi. –

38

Giải pháp tốt nhất dành cho iOS 9 trở lên

Điều này là do của một featu mới được gọi là hướng dẫn nội dung có thể đọc được. Nó cung cấp các lề phù hợp cho việc đọc. Vì vậy, trong iPhone và iPad chân dung, chúng có tỷ suất rất nhỏ, nhưng trong iPad cảnh quan chúng lớn hơn. Trong iOS 9, lề của ô UITableView mặc định theo hướng dẫn nội dung có thể đọc được.

Nếu bạn muốn dừng điều đó, chỉ cần đặt tableView's cellLayoutMarginsFollowReadableWidth thành NO/false.

Nguồn:https://forums.developer.apple.com/thread/5496

+2

Câu trả lời rất hay! Rất tiếc là nó chưa được ghi lại. Câu hỏi đó xảy ra khi tôi đang chuẩn bị [mỏ] (http://stackoverflow.com/questions/31537196/ios-9-uitableview-separators-insets-significant-left-margin/) –

3

Một chút muộn. Tôi hy vọng là hữu ích cho người khác ...

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

readableContentGuide là một hướng dẫn bố trí đó đã được thêm vào mỗi UIView

Điều này là để đảm bảo người dùng không cần phải quay đầu lại để đọc Nội dung.

Nếu bạn muốn làm theo các hướng dẫn nội dung có thể đọc được, sau đó làm như sau:

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 

Lưu ý: Trong đoạn mã trên đáy và neo đầu sử dụng contentView thay vì readableContentGuide để các nội dung biên dọc thay đổi dựa trên số tableView.rowHeight.

Các vấn đề liên quan