【问题标题】:Swift UIView zPosition issue, views overlap on other viewsSwift UIView zPosition 问题,视图与其他视图重叠
【发布时间】:2021-05-01 00:33:55
【问题描述】:

我正在写一个日历视图,我在它的超级视图中添加了其他三个视图,所以日历视图应该在顶层,在模拟器上看起来很好

但是后来发现无法点击按钮,然后查看视图层级,发现其他三个视图在日历上重叠,就像模拟器上的3d预览和视图一样奇怪不一样的

这是UI部分的代码

func updateUI() {

    if calendarView != nil {

        self.view.addSubview(calendarView!)

        self.calendarView!.frame.origin = calendarViewPosition!
        self.calendarView!.layer.zPosition = 5
        self.calendarView!.layer.cornerRadius = self.setting.itemCardCornerRadius
        self.calendarView!.layer.masksToBounds = true
        self.calendarView!.setViewShadow()
        UIView.animate(withDuration: 0.8, delay: 0, options: .curveEaseOut, animations: {
            self.calendarView!.frame.origin.y += 50
        }) { _ in
            
            var newCalendarPageCordiateYDifference: CGFloat = 10
            var newCalendarPageSizeDifference: CGFloat = 20
            var zPosition: CGFloat = 4
            
            for _ in 1 ... 3 {
                let newCalendarPage = UIView()
            
                newCalendarPage.frame = CGRect(x: self.calendarView!.frame.origin.x + newCalendarPageSizeDifference / 2, y: self.calendarView!.frame.origin.y, width: self.calendarView!.frame.width - newCalendarPageSizeDifference, height: self.calendarView!.frame.height - newCalendarPageSizeDifference)
                newCalendarPage.backgroundColor = .white
                newCalendarPage.layer.zPosition = zPosition
                newCalendarPage.layer.cornerRadius = self.setting.itemCardCornerRadius
                newCalendarPage.setViewShadow()
                
                self.view.addSubview(newCalendarPage)
                
                UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
                    newCalendarPage.frame.origin.y -= newCalendarPageCordiateYDifference
                })
                
                zPosition -= 1
                newCalendarPageCordiateYDifference += 10
                newCalendarPageSizeDifference += 50
            }
            
           
        }
    }
    
    
   
    

}

【问题讨论】:

    标签: swift uiview layer zposition


    【解决方案1】:

    从 for 循环中删除 self.view.addSubview(newCalendarPage) this 并 像这样添加子视图

    self.view.insertSubview(newCalendarPage, belowSubview: calendarView)
    

    另一种方法是禁用用户交互,例如 newCalendarPage.isUserInteractionEnabled = false

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多