【问题标题】:UIBezierPath fill with an offset in a custom UIView in a UITableViewCellUIBezierPath 在 UITableViewCell 中的自定义 UIView 中填充偏移量
【发布时间】:2015-09-25 05:50:22
【问题描述】:

我有一个自定义 UIView 在视图中绘制一个实心圆角矩形。问题是当我将视图放在 UITableViewCell 中并放置约束时,运行时它不会在我放置它的位置绘制圆角矩形。

以下是图片供参考:http://imgur.com/a/XwCHS

前 2 个在 Xcode 中,而第 3 个在 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
    }
}

【问题讨论】:

    标签: ios swift uitableview uiview uibezierpath


    【解决方案1】:

    可能是您的约束设置不正确。尝试在 Interface Builder 中布置圆角矩形并在运行前设置约束。

    1. 清除圆角矩形上的所有现有约束
    2. 按照您想要的方式在屏幕上布置圆角矩形
    3. 添加缺少的约束

    试一试。

    【讨论】:

      【解决方案2】:

      替换

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

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

      看看有没有用

      【讨论】:

      • 这成功了!感谢您的解决方案,我不知道为什么 self.frame 会发生变化,但我应该足够聪明,可以在 drawRect 中使用给定的 rect
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多