簡體   English   中英

UIView遮罩無法與自動布局一起正常使用

[英]UIView Masking not working correctly with autolayout

我試圖掩蓋UITableView的底部和頂部。 我發現以下代碼適用於特定的屏幕尺寸(iPhone 6)並產生以下結果:

在此處輸入圖片說明

通過應用以下代碼可以實現:

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
    }

如您所見,我添加了一個代碼段來嘗試使轉角半徑與該行中的屏幕尺寸(實際上是視圖尺寸)成比例:

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

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
    }

當我在iPhone 6+(和較小的iPhone 5)屏幕上運行該應用程序時,該視圖被不正確地屏蔽:

在此處輸入圖片說明

關於如何解決的任何想法?

謝謝 :)

cellForRowAtIndexPath您正在使用自定義單元格,並在方法中給出角半徑:

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

...您正在使用UITableViewCell 嘗試更改為customCell類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM