UIViewController中的子视图高度

时间:2018-08-15 13:32:23

标签: ios swift

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

在应用程序中,我添加了情节提要和每个viewcontroller都有一个标题。 我必须根据设备大小来改变Header的高度,所以我创建了如下的类

@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
    }
}

情节提要中的每个标题都继承了该类。该课程与情节提要完美配合。例如,iPhone 8 plus的视图高度为148.45

现在我正在尝试在没有情节提要(用Objective-c编写)的ViewController中使用同一类

但视图高度始终为115.0

resetViewHeight is : 115.0

请建议在没有情节提要类的情况下如何更改标题视图的高度。

任何想法将不胜感激。

1 个答案:

答案 0 :(得分:1)

目前尚不清楚OP在做什么,但根据注释,这是一个有效的示例:


//
//  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),

            ])

    }

}