2017-08-06 14 views
2

@IBDesignable UICollectionViewCellプロトタイプに薄い色の枠線を追加しようとしています。ストーリーボードでは、layer.cornerRadius,layer.masksToBounds、およびlayer.borderWidthというユーザー定義のプロパティを設定しており、境界線が予想どおりに表示されます。layer.borderColorが設定されているとUICollectionViewCellボーダーが消えます

enter image description here

しかし、私はまた、ストーリーボードから消えカスタムlayer.borderColor、全体の国境、半径、およびマスクを設定している場合。シミュレータでは、角の半径(とマスク、おそらく)が表示されますが、境界線は表示されません。

enter image description here

私もどちらか、動作しないよう、プログラム的にそれらを設定するが、this year-old, unanswered StackOverflow questionが示すように試してみました。

要求されるようにここでは、セルのためのコードです:

@IBDesignable open class ReleaseSpineCell: UICollectionViewCell { 

    public var masterRelease: MasterRelease? { 
     didSet { 
      artistName = masterRelease?.artistName 
      title = masterRelease?.title 
      catalogNumber = "none" 
     } 
    } 

    @IBInspectable public var artistName: String? { 
     didSet { 
      artistLabel?.text = artistName 
     } 
    } 

    @IBInspectable public var title: String? { 
     didSet { 
      titleLabel?.text = title 
     } 
    } 

    @IBInspectable public var catalogNumber: String? { 
     didSet { 
      catalogLabel?.text = catalogNumber 
     } 
    } 

    @IBOutlet private weak var artistLabel: UILabel? 
    @IBOutlet private weak var titleLabel: UILabel? 
    @IBOutlet private weak var catalogLabel: UILabel? 

} 

そしてUICollectionViewControllerdataSourceの関連部分:

class CollectionModel: FetchedResultsCollectionModel { 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "spineCell", for: indexPath) as! ReleaseSpineCell 
     cell.masterRelease = fetchedResultsController?.fetchedObjects?[indexPath.row] as? MasterRelease 

     return cell 
    } 
} 

FetchedResultsCollectionModelだカスタム・コレクション・ビューのデータソースとデリゲートでありますNSFetchedResultsControllerでサポートされ、MasterReleaseは管理オブジェクトです。私はこのようなものを可能な限りシンプルに保つように努めています。

+1

に役立ちますか? –

+0

確かに、それはほんの一握りのIBOutletです。興味深いコードはありません。 – NRitH

答えて

1

Booleanの値がlayer.borderColorの場合は、代わりにColorを使用する必要があります。 layer.borderColorのためにあなたがcgColorを必要とするので、しかし、いずれにせよ、これはので、私はあなたが

@IBDesignable open class ReleaseSpineCell: UICollectionViewCell { 

    public var masterRelease: MasterRelease? { 
     didSet { 
      artistName = masterRelease?.artistName 
      title = masterRelease?.title 
      catalogNumber = "none" 
     } 
    } 

    @IBInspectable public var artistName: String? { 
     didSet { 
      artistLabel?.text = artistName 
     } 
    } 

    @IBInspectable public var title: String? { 
     didSet { 
      titleLabel?.text = title 
     } 
    } 

    @IBInspectable public var catalogNumber: String? { 
     didSet { 
      catalogLabel?.text = catalogNumber 
     } 
    } 

    //added this inspectable property 
    @IBInspectable public var borderColor: UIColor? { 
     didSet { 
      self.layer.borderColor = borderColor!.cgColor 
     } 
    } 

    @IBOutlet private weak var artistLabel: UILabel? 
    @IBOutlet private weak var titleLabel: UILabel? 
    @IBOutlet private weak var catalogLabel: UILabel? 

} 

その後、あなたはストーリーボードでこれを行うことができるようになりますBORDERCOLORため@IBInspectable VARを定義し、コードでこれを行う必要があると思い動作しません。

enter image description here

希望これはあなたのカスタムセルのためのあなたのコードを投稿することができ

+0

ああ、私が 'layer.borderColor'に対して持っていた' Boolean'型の良い呼び出しです。しかし、私はちょうど間違ったスクリーンショットをアップロードしました - 私はそれを 'カラー 'に設定しました。 'Color'がユーザ定義の属性の型として指定されているとき、IBはそれが本当に' CGColor'であることを知っているので、それは問題ではないと思います。しかし、私はあなたの例から、あなたがそれを働かせていることを知ることができるので、私は何か間違ったことをしています。どのXcodeバージョンを使用していますか?私は9ベータ4です。 – NRitH

+0

@NRitHあなたが私の答えで言ったように、inspectableプロパティとしてborderColorを追加しようとしましたか?とにかく、Xcodeを使ってIamを動作させる必要があります8.3.2 –

+0

はい、inspectableプロパティあなたが提案したように。私は他のプロジェクトでこの仕事をしてきたことを知っています。私はXcode 9ベータ4のバグまでチョークするつもりです。 – NRitH

関連する問題