Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIViewController中的子视图高度_Ios_Swift - Fatal编程技术网

Ios UIViewController中的子视图高度

Ios UIViewController中的子视图高度,ios,swift,Ios,Swift,我正在开发一个应用程序,在这个应用程序中,我有一些没有情节提要和自动布局的ViewController,我在自动布局情节提要中添加了其他功能 在应用程序中,我添加了故事板&每个viewcontroller都有一个标题。 我必须根据设备大小改变标题的高度,所以我创建了一个类,如下所示 @objc class CommonHeaderView: UIView { override func layoutSubviews() { super.layoutSubviews()

我正在开发一个应用程序,在这个应用程序中,我有一些没有情节提要和自动布局的ViewController,我在自动布局情节提要中添加了其他功能

在应用程序中,我添加了故事板&每个viewcontroller都有一个标题。 我必须根据设备大小改变标题的高度,所以我创建了一个类,如下所示

@objc class CommonHeaderView: UIView {
    override func layoutSubviews() {
        super.layoutSubviews()
        //
        resetViewHeight()
        print("resetViewHeight is :\(self.frame.height)")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        resetViewHeight()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    //MARK:- Reset Height as per device size
    func resetViewHeight()
    {
        self.heightAnchor.constraint(equalTo: self.widthAnchor, multiplier: CGFloat(115.0/320.0), constant: 0).isActive = true
    }
}
故事板中的每个标题都继承了这个类。这个类与故事板完美结合。例如,iPhone8Plus中的视图高度为148.45

现在我尝试在ViewController中使用相同的类,而不使用故事板(用objective-c编写)

但视图高度始终为115.0

resetViewHeight is : 115.0
请建议如何在没有故事板类的情况下改变headerview的高度


任何想法都将不胜感激

目前尚不清楚OP到底做错了什么,但根据评论,这里有一个工作示例:




这对我来说似乎很好。。。在iPhone7上运行时,我得到的重置视图高度是:135.0@DonMag你是怎么尝试的?您是否尝试过在没有情节提要的UIViewController子类中使用。请你能展示你的作品吗?是的,没有故事板@多谢。让我查一查。感谢您的帮助。@DonMag您能否在答案中添加您的代码。我会记为正确答案。
//
//  AppDelegate.swift
//
//  Created by Don Mag on 8/15/18.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow(frame: UIScreen.main.bounds)
        let testHViewController = TestHViewController()
        testHViewController.view.backgroundColor = UIColor.red
        window!.rootViewController = testHViewController
        window!.makeKeyAndVisible()
        return true

    }
}
//
//  TestHViewController.swift
//
//  Created by Don Mag on 8/15/18.
//

import UIKit

@objc class CommonHeaderView: UIView {
    override func layoutSubviews() {
        super.layoutSubviews()
        //
        resetViewHeight()
        print("resetViewHeight is :\(self.frame.height)")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        resetViewHeight()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    //MARK:- Reset Height as per device size
    func resetViewHeight()
    {
        self.heightAnchor.constraint(equalTo: self.widthAnchor, multiplier: CGFloat(115.0/320.0), constant: 0).isActive = true
    }
}

class TestHViewController: UIViewController {

    let hView: CommonHeaderView = {
        let v = CommonHeaderView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = .blue
        return v
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(hView)

        NSLayoutConstraint.activate([

            hView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0),
            hView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0),
            hView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0),

            ])

    }

}