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

标签 ios swift uitableview uiview uibezierpath

我有一个自定义 UIView,它在 View 中绘制一个填充的圆角矩形。问题是,当我将 View 放置在 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
    }
}

最佳答案

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

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

试一试。

关于ios - UIBezierPath 填充 UITableViewCell 中自定义 UIView 中的偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32775648/

相关文章:

在 Windows 上使用 Flutter 进行 iOS 开发?

ios - 实现 iOS 7.1 UINavigationBar 模糊

ios - NSNotificationCenter 作为整个应用程序的主要事件调度器

ios - 在 Swift 中从 Uibezierpath 数组创建一个 UIImage

ios - 如果我有不同的 UI 控件要顺序显示,我是否仍然应该使用 TableView Controller ?

iphone - 动画化 UITableView 部分标题的自定义 View

ios - Oauth1 错误常量 Swift iO

ios - collectionView : reloadData works, reloadItemsAtIndexPaths 不

ios - TableView 单元格阴影未出现

ios - 格式说明符