简体   繁体   English

如何在 Swift/iOS 中获取以编程方式添加的视图的宽度和高度?

[英]How to fetch the width and height of a programatically added view in Swift/ iOS?

I have added a UIView and a UIImageView programatically, but I need its width and height.我以编程方式添加了 UIView 和 UIImageView,但我需要它的宽度和高度。 Is it possible to fetch it, if yes, please share how to fetch it.是否可以获取它,如果可以,请分享如何获取它。

Code:代码:

let View1: UIView = {
            let viewView = UIView()
            viewView.translatesAutoresizingMaskIntoConstraints = false
            viewView.contentMode = .scaleAspectFit
            print(UserDefaults.standard.bool(forKey: "backgroundColourSelected"))
            if UserDefaults.standard.bool(forKey: "backgroundColourSelected") {
                viewView.backgroundColor = self.viewColor
            }else {
                viewView.backgroundColor = .white
            }
            viewView.clipsToBounds = true
            return viewView
        }()
    
        self.canvasView.addSubview(View1)
        
        View1.centerXAnchor.constraint(equalTo: canvasView.centerXAnchor, constant: 0).isActive = true
        View1.centerYAnchor.constraint(equalTo: canvasView.centerYAnchor, constant: 0).isActive = true
        View1.widthAnchor.constraint(equalTo: canvasView.widthAnchor, constant: 0).isActive = true
        View1.heightAnchor.constraint(equalTo: canvasView.widthAnchor, multiplier: aspectRatio).isActive = true

use override func viewWillLayoutSubviews() for ViewController and override func layoutSubviews() for UIView.对 ViewController 使用覆盖 func viewWillLayoutSubviews() 并为 UIView 使用覆盖 func layoutSubviews()。

Example: - for UIViewControllers示例:- 用于 UIViewControllers

override func viewWillLayoutSubviews() {
    print(yourView.frame.height)
}

for UIViews对于 UIViews

override func layoutSubviews() {
    print(yourView.frame.height)
    print(yourView.frame.width)

}

Since your view is inside the function just move it out由于您的视图在 function 内部,只需将其移出

class YourController / YourView {
private let imgView: UIImageView = {
    let iv = UIImageView()
    // do whatever you want with your image using iv. eg. iv.contentMode = .scaleAspectFit or iv.image = UIImage(named: "custom_img")
    return iv
}()

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(imgView)
    //set constraints for your
}

// then use layoutSubViews(for views) / viewWillLayoutSubViews (for controller) and calculate the height

} }

You need to know the life cycle of view controller.你需要知道视图controller的生命周期。

To get height of any view use viewDidLayoutSubviews .要获得任何视图的高度,请使用viewDidLayoutSubviews

override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()

      print(view1.frame.size)
}

size property will print both final height and width. size属性将打印最终高度和宽度。

暂无
暂无

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

相关问题 如何以编程方式为iOS中的集合视图单元格提供宽度和高度,Swift 3 - How to give width and height programatically for collection view cell in ios, swift 3 以编程方式更改视图的高度和宽度会导致使用swift在iOS中出现意外行为 - Changing Height and Width of View programatically leads to unexpected behaviour in iOS using swift 如何在Swift iOS(Xcode 6.0)中以编程方式更改UIWebView高度 - How to change the UIWebView height Programatically in Swift iOS (Xcode 6.0) 如何在iOS中动态更改视图的高度和宽度? - How to dynamically change the height & width of view in iOS? Swift 4,iOS如何在添加字符串值后检测标签高度 - Swift 4, iOS how to detect label height after added string value Swift - IOS:如何从布局中测量项目的高度和宽度 - Swift - IOS : How to measure height and width of item from within layout iOS | 如何在swift中获取运行时图像的宽度/高度 - iOS | How to get runtime image width/height in swift 如何在Swift中未知宽度和高度的视图中居中放置UIBezierPath椭圆? - How to center a UIBezierPath oval within a view of unknown width and height in Swift? iOS:以编程方式添加约束作为唯一指标来获取视图高度 - iOS: Getting height of views with programatically added constraints as only indicator 如何在iOS中的autolayout中设置视图的动态宽度和高度? - How to set dynamic width & height of a view in autolayout in iOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM