简体   繁体   English

UIView遮罩无法与自动布局一起正常使用

[英]UIView Masking not working correctly with autolayout

I'm trying to mask the bottom and top corner of a UITableView. 我试图掩盖UITableView的底部和顶部。 I've found that the following code works for a specific screen size (iPhone 6) and produces these results: 我发现以下代码适用于特定的屏幕尺寸(iPhone 6)并产生以下结果:

在此处输入图片说明

It's achieved by applying the following code: 通过应用以下代码可以实现:

class func maskRowForIndexPath<T>(cell : T ,indexPath: NSIndexPath, count : Int) -> T{
        let const = UIScreen.mainScreen().bounds.width * (6 / (cell as! UIView).frame.size.width)
        let cornerRadii = CGSizeMake(const, const)
        let row = indexPath.row
        if row == 0 {
            let maskLayer = CAShapeLayer()
            maskLayer.path = UIBezierPath(roundedRect: (cell as! UITableViewCell).bounds, byRoundingCorners: UIRectCorner.TopLeft.union(.TopRight), cornerRadii: cornerRadii).CGPath
            (cell as! UITableViewCell).layer.mask = maskLayer
        }

        if row == count-1{
            let maskLayer = CAShapeLayer()
            maskLayer.path = UIBezierPath(roundedRect: (cell as! UITableViewCell).bounds, byRoundingCorners: UIRectCorner.BottomLeft.union(.BottomRight), cornerRadii: cornerRadii).CGPath
            (cell as! UITableViewCell).layer.mask = maskLayer
        }
        return cell
    }

As you can see, I added a snippet to try and make the corner radii proportional to the screen size (and essentially the view size) in this line: 如您所见,我添加了一个代码段来尝试使转角半径与该行中的屏幕尺寸(实际上是视图尺寸)成比例:

    let const = UIScreen.mainScreen().bounds.width * (6 / (cell as! UIView).frame.size.width)

The maskRowForIndexPath method is calling inside my UITableView cellForRowAtIndexPath method: maskRowForIndexPath方法正在我的UITableView cellForRowAtIndexPath方法内部调用:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let row = indexPath.row
        let info = (indexPath.section == 0) ? firstSectionTitles : secondSectionTitles
        let cell : AccountHomeTableViewCell = tableView.dequeueReusableCellWithIdentifier("AccountHomeTableViewCell") as! AccountHomeTableViewCell
        cell.titleLabel.attributedText = attributedTitleForCellString(info[row].title)
        cell.cellImageView.image = UIImage(named: info[row].imageName)
        cell.layoutMargins = UIEdgeInsetsZero;
        UIView.maskRowForIndexPath(cell, indexPath: indexPath, count: info.count)

        return cell
    }

When I run the app on an iPhone 6+ (and smaller iPhone 5) screen, the view is masked improperly: 当我在iPhone 6+(和较小的iPhone 5)屏幕上运行该应用程序时,该视图被不正确地屏蔽:

在此处输入图片说明

Any idea on how to solve? 关于如何解决的任何想法?

Thanks :) 谢谢 :)

In your cellForRowAtIndexPath you are using your custom cell, and to give corner radii in your method: cellForRowAtIndexPath您正在使用自定义单元格,并在方法中给出角半径:

class func maskRowForIndexPath<T>(cell : T ,indexPath: NSIndexPath, count : Int) -> T

...you are using UITableViewCell . ...您正在使用UITableViewCell Try changing to your customCell class. 尝试更改为customCell类。

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

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