【问题标题】:UIView default properties and custom propertiesUIView 默认属性和自定义属性
【发布时间】:2018-10-16 03:11:49
【问题描述】:

我用默认属性创建了 UIView

var color =  Label.Color.black { didSet{ setNeedsDisplay() }}
var count =  Label.Count.one.rawValue   { didSet{ setNeedsDisplay() }}
var shading =  Label.Shading.empty   { didSet{ setNeedsDisplay() }}
var shape =  Label.Shape.triangle { didSet{ setNeedsDisplay() }}
var isSelected = false

然后我在 ViewController 中使用相同的 UIView 添加一些子视图

func addCardsOnGridView() {
    grid.frame = cardsView.bounds
    grid.cellCount = game.cardsOnDeck.count
    for cardView in cardsView.subviews{

        cardView.removeFromSuperview()
    }
    for index in 0..<grid.cellCount {
        if let cellFrame = grid[index] {
            let card = game.cardsOnDeck[index]
            let cardView = CardsView(frame: cellFrame.insetBy(dx: CardSize.inset, dy: CardSize.inset))
            cardView.color = card.label.color
            cardView.count = card.label.count.rawValue
            cardView.shape = card.label.shape
            cardView.shading = card.label.shading
            //cardView.isSelected = card.isSelected
            //print(cardsView.isSelected)
            cardsView.addSubview(cardView)

        } else {
            print("grid[\(index)] doecnt exist")
        }
    }
}

然后我得到了这个默认的 UIView 作为 superview 和 superview 之上的子视图:

如何删除具有默认 UIView 属性的视图并使用自定义属性重绘?

【问题讨论】:

  • 你到底是什么意思?哪个观点有问题?
  • @Edward 那么使用大三角形吗?它是具有默认属性的视图。我们有 24 个带有自定义视图的小视图。我想知道如何删除带有大三角形的视图并只显示自定义视图
  • 所以我想用自定义值覆盖 UIView 的默认值

标签: ios swift uiview viewcontroller


【解决方案1】:

我无法完全理解您的问题。我的理解是你想要获得像三角形视图这样的特定视图。

实现这一点的一种方法是在 mainView 中添加 tag 给每个子视图,如下所示

aView.tag = 6

然后当你想得到一个子视图时,你可以使用viewWithTag 函数,如下所示

if let aCustomView = mainView.viewWithTag(6) as? CustomView {
   aCustomView.myColor = .blue
}

另一种获取自定义视图的方法是循环思考子视图并尝试将视图转换为自定义类,如下所示

mainView.subviews.forEach({ (aView) in
  if aView.tag == 6,
     let customView = aView as? CustomView {
        customView.myColor = .blue
  }
}) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多