繁体   English   中英

如何将UIView放在其他UIView之上?

[英]How to place UIView on top of other UIView?

现在,我有一个collectionView,每个单元格都包含一个水平stackView。 stackView填充了一系列UIView(矩形),一个月的每一天-每个单元格对应一个月。 我这样填充堆栈视图:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == self.collectionView {
            ...
            return cell
        } else if collectionView == self.timeline {
            let index = indexPath.row
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MMM"
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: timelineMonthCellReuseIdentifier, for: indexPath) as! SNTimelineMonthViewCell
            let firstPost = posts.first?.timeStamp
            let month = Calendar.current.date(byAdding: .month, value: index, to: firstPost!)
            print(dateFormatter.string(from: month!),dateFormatter.string(from: firstPost!),"month diff")
            for post in posts {
                print(post.timeStamp, "month diff")
            }
            cell.monthLabel.text = dateFormatter.string(from: month!)
            cell.monthLabel.textAlignment = .center

            if let start = month?.startOfMonth(), let end = month?.endOfMonth(), let stackView = cell.dayTicks {
                var date = start
                while date <= end {
                    let line = UIView()
                    if posts.contains(where: { Calendar.current.isDate(date, inSameDayAs: $0.timeStamp) }) {
                        line.backgroundColor = UIColor(red:0.15, green:0.67, blue:0.93, alpha:1.0)
                        let tapGuesture = UITapGestureRecognizer(target: self, action:  #selector (self.tapBar (_:)))
                        line.isUserInteractionEnabled = true
                        line.addGestureRecognizer(tapGuesture)
                        self.dayTicks[date] = line
                    } else {
                        line.backgroundColor = UIColor.clear
                    }
                    stackView.addArrangedSubview(line)
                    date = Calendar.current.date(byAdding: .day, value: 1, to: date)!
                }
            }
            return cell
        } else {
            preconditionFailure("Unknown collection view!")
        }
    }

然后,当用户停止滚动其他集合视图时,我想在dayTick的顶部添加一个名为arrowView的子视图(请参见self.dayTicks如何用上面stackView的子视图填充)。

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let currentIndex = self.collectionView.contentOffset.x / self.collectionView.frame.size.width
        let post = posts[Int(currentIndex)]
        for (_,tick) in self.dayTicks {
            tick.subviews.forEach({ $0.removeFromSuperview() })
        }
        let day = Calendar.current.startOfDay(for: post.timeStamp)
        let tick = self.dayTicks[day]
        let arrow = UIImage(named:"Tracer Pin")
        let arrowView = UIImageView(image: arrow)
//        arrowView.clipsToBounds = false
        print((tick?.frame.origin)!,"tick origin")
//        arrowView.frame.origin = (tick?.frame.origin)!
//        arrowView.frame.size.width = 100
//        arrowView.frame.size.height = 100
        tick?.addSubview(arrowView)

    }

这种作品,看起来像这样:

在此处输入图片说明

添加了红色矩形,但它显示在dayTick的右侧,并且显示为细长的矩形。 实际上,引用的Tracer Pin图像如下所示:

在此处输入图片说明

那至少是红色的来源,但是正如您看到的那样,它延伸得很奇怪,并且剪裁了不在矩形UIView空间中的所有内容。

现在请注意,我注释掉了设置arrowView的大小和原点以及将clipToBounds设置为false的4行。 当我取消注释这些行时-arrowView根本不会显示,因此我必须做错了。 我想要显示的是这样的:

在此处输入图片说明

我怎么能直接放在上面呢?

看起来您的arrowView高度固定。 可能是红色三角形部分处于另一视角下?

调试视图层次结构

单击调试视图层次结构,它是右图标中的第二个-它看起来像3个矩形。 检查整个图像是否在那里。

另一个角度可能是使用CALayer执行此操作。 以下是一些提示(来自其他项目),可帮助您发现解决方案:

@IBInspectable open var slideIndicatorThickness: CGFloat = 5.0 {
    didSet {
        if slideIndicator != nil { slideIndicator.removeFromSuperlayer() }
        let slideLayer = CALayer()
        let theOrigin = CGPoint(x: bounds.origin.x, y: bounds.origin.y)
        let theSize = CGSize(width: CGFloat(3.0), height: CGFloat(10.0)
        slideLayer.frame = CGRect(origin: theOrigin, size: theSize)
        slideLayer.backgroundColor = UIColor.orange.cgColor
        slideIndicator = slideLayer
        layer.addSublayer(slideIndicator)
    }
}

fileprivate var slideIndicator: CALayer!

fileprivate func updateIndicator() {
    // ..
    // Somehow figure out new frame, based on stack view's frame.
    slideIndicator.frame.origin.x = newOrigin
}

您可能必须在UIStackView的子类或您自己的自定义视图上实现此方法,该视图是UIStackView的包装器。

我最终使用了CALayer-感谢@ouni的建议出于某种原因,CALayer似乎直接在视图顶部绘制,而子视图却没有。 一键是取消选中collectionView单元格本身(而不是子视图)上的“ Clip to bounds”框-这样我就可以在collection view单元格外部绘制箭头的喇叭形底部:

在此处输入图片说明

我的代码如下所示:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if scrollView == self.collectionView {
            print("is collection view")
            print(scrollView,"collection view")
            let currentIndex = self.collectionView.contentOffset.x / self.collectionView.frame.size.width
            let post = posts[Int(currentIndex)]
            for (_,tick) in self.dayTicks {
                tick.layer.sublayers = nil
            }
            let day = Calendar.current.startOfDay(for: post.timeStamp)
            let tick = self.dayTicks[day]
            let arrowLayer = CAShapeLayer()
            let path = UIBezierPath()
            let start_x = (tick?.bounds.origin.x)!
            let start_y = (tick?.bounds.minY)!
            let top_width = (tick?.bounds.width)!
            let tick_height = (tick?.bounds.height)!
            let tip_height = CGFloat(10)
            let tip_flare = CGFloat(10)
            path.move(to: CGPoint(x: start_x, y: start_y))
            path.addLine(to: CGPoint(x: start_x + top_width,y: start_y))
            path.addLine(to: CGPoint(x: start_x + top_width,y: start_y + tick_height))
            path.addLine(to: CGPoint(x: start_x + top_width + tip_flare,y: start_y+tick_height+tip_height))
            path.addLine(to: CGPoint(x: start_x - tip_flare,y: start_y + tick_height + tip_height))
            path.addLine(to: CGPoint(x: start_x,y: start_y+tick_height))
            path.close()
            arrowLayer.path = path.cgPath
            arrowLayer.fillColor = UIColor(red:0.99, green:0.13, blue:0.25, alpha:1.0).cgColor
            tick?.layer.addSublayer(arrowLayer)         
        } else {
            print(scrollView, "timeline collection view")
        }


    }

这会在子视图的顶部漂亮地绘制箭头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM