现在,我有一个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)

    }

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

ios - 如何将UIView放在其他UIView之上?-LMLPHP

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

ios - 如何将UIView放在其他UIView之上?-LMLPHP

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

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

ios - 如何将UIView放在其他UIView之上?-LMLPHP

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

最佳答案

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

Debug View Hierarchy

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

10-08 06:24