swift - 看不到 UIView 的 UIView subview

标签 swift uikit

我看不到添加到 UIView 的 UIView subview 。我可以在 View 层次结构中清楚地看到它,但我在应用程序运行时看不到它。

代码:

var magnifyingView: UIView = {
    let view = UIView()
    view.alpha = 1.0
    view.backgroundColor = UIColor.yellow
    return view
}()

guard let cgImage = capturedImage else{return}
    let image = UIImage.init(cgImage: cgImage, scale: 1.0, orientation: UIImage.Orientation.right)
    imageView.image = image
    self.addSubview(imageView)
    bottomSection.addSubview(homeButton)
    bottomSection.addSubview(retakeButton)
    bottomSection.addSubview(continueButton)
    self.addSubview(bottomSection)

    showMagifyingView()
    self.bringSubviewToFront(magnifyingView)

private func showMagifyingView(){
    magnifyingView = UIView()
    magnifyingView.frame = CGRect.init(x: 30, y: 30, width: 200, height: 200)
    self.addSubview(magnifyingView)

    magnifyingView.translatesAutoresizingMaskIntoConstraints = false
    magnifyingView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.3).isActive = true
    magnifyingView.heightAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.3).isActive = true
    magnifyingView.topAnchor.constraint(equalTo: self.topAnchor, constant: 30).isActive = true
    magnifyingView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -30).isActive = true

}

View Heirarchy(蓝色突出显示的矩形 UIview 是我在运行时在我的应用程序上看不到的内容。即使它在那里并且我已将它放在前面)

enter image description here

最佳答案

在类的顶部初始化magnifyingView

var magnifyingView: UIView = {
    let view = UIView()
    view.alpha = 1.0
    view.backgroundColor = UIColor.yellow
    return view
}()

但是您要在 showMagifyingView() 中添加一个不同的 magnifyingView = UIView() 作为 subview 。

关于swift - 看不到 UIView 的 UIView subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58695196/

相关文章:

swift - NSTableView 不会调用 viewForTableColumn

ios - 将 AVI 视频转换为 MP4 视频(用于在 iOS 上播放)

ios - 如何子类化 UICollectionViewFlowLayout 以便可以覆盖 layoutAttributesForElementsInRect

ios - 启动屏幕问题

ios - 在另一个应用程序中打开文件时更改 UIDocumentInteractionController 中的文件名

swift - 需要按取消按钮两次才能转到上一个 View Controller

ios - 在 Objective-C 文件中调用 Swift API

ios - 来自不同状态的颜色的 UIButton 背景图像

iphone - 在 UICollectionView/UICollectionViewLayout 旋转或边界更改时禁用淡入淡出的最佳方法是什么?

ios - UITableView:滑动RowAction将取消选定的行