UIBezierPath在UITableViewCell中的自定义UIView中填充偏移量



我有一个自定义UIView,它在视图中绘制一个填充的圆角矩形。问题是,当我把视图在我的UITableViewCell和放置约束,当运行时,它不画圆角矩形在我放置它的地方。

以下图片供参考:https://i.stack.imgur.com/hy3W8.jpg

前两个是在Xcode中,而第三个是在iPhone 5模拟器上运行的。

我的定制UIView:

import UIKit
@IBDesignable class AlertRoundedView: UIView {
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
        let color : UIColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
        color.setFill()
        let path : UIBezierPath = UIBezierPath(roundedRect: CGRectInset(self.frame, 0, 0), cornerRadius: 20)
        path.fill()
    }

}

我的TableViewController(只是用于测试现在):

import UIKit
class AlertsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
    @IBOutlet weak var emptyAlertsView: UIView!
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        var alerts : [Int] = [4, 6, 3, 4]
        if(alerts.count > 0){
            //Show table
            tableView.hidden = false
            emptyAlertsView.hidden = true
            tableView.delegate = self
            tableView.dataSource = self
            tableView.allowsSelection = false
            tableView.reloadData()
        }else{
            //Show empty view
            tableView.hidden = true
            emptyAlertsView.hidden = false
        }

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 4
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("AlertCell", forIndexPath: indexPath)
        print(cell)
        // Configure the cell...
        return cell
    }
}

可能是您的约束设置不正确。在运行之前,请尝试在界面生成器中布局圆角矩形并设置约束。

  1. 清除四舍五入矩形的所有约束
  2. 按你想要的方式在屏幕上布局圆角矩形
  3. 添加缺失的约束

试试吧

replace

let path : UIBezierPath = UIBezierPath(roundedRect: CGRectInset(self.frame, 0, 0), cornerRadius: 20)

let path : UIBezierPath = UIBezierPath(roundedRect: CGRectInset(rect, 0, 0), cornerRadius: 20)

看看是否可以

最新更新