简体   繁体   English

Swift:更改 UINavigationController 视图的高度

[英]Swift: Change height of UINavigationController view

I have a UITableView embedded in a UINavigationController.我有一个 UITableView 嵌入在 UINavigationController 中。 The UITableView is set up as follows: UITableView 设置如下:

import UIKit


class HistoryTableViewController: UITableViewController {

let cellID = "HistoryColumns"

struct CellData {
    let date: String?
    let number: String?
    let before: String?
    let after: String?
    let diff: String?
    let strength: String?
    let location: String?
}

var data = [CellData]()

override func viewDidLoad() {
    super.viewDidLoad()
    
    data = [
        // data goes here
    ]
    tableView.register(HistoryTableViewCell.self, forCellReuseIdentifier: cellID)


    self.navigationController?.navigationBar.topItem?.title = "Recent Activity"
}


// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    return data.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as! HistoryTableViewCell
    if indexPath.row % 2 == 0 {
        cell.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
    }
    
    cell.date.valueLabel.text = data[indexPath.row].date
    cell.number.valueLabel.text = data[indexPath.row].flightNumber
    cell.before.valueLabel.text = data[indexPath.row].preFuel
    cell.after.valueLabel.text = data[indexPath.row].postFuel
    cell.diff.valueLabel.text = data[indexPath.row].uplift
    cell.strength.valueLabel.text = data[indexPath.row].density
    cell.location.valueLabel.text = data[indexPath.row].location
    
    return cell
}
}

I call the controller within a UINavigationController as follows:我在 UINavigationController 中调用 controller 如下:

    @objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
    DispatchQueue.asyncMain {
        let history = HistoryTableViewController()
        let navigationController = UINavigationController(rootViewController: history)
    
        self.present(navigationController, animated: true, completion: nil)
        
    }
}

What I want to do is to define the height of the navigationController's view here (around 500).我要做的是在这里定义navigationController的视图高度(大约500)。 I've tried using navigationController.view.heightAnchor.constraint but it had no effect (obviously I set translatesAutoresizingMaskIntoConstraints to false first)...我试过使用 navigationController.view.heightAnchor.constraint 但它没有效果(显然我首先将 translatesAutoresizingMaskIntoConstraints 设置为 false)......

I do want to use a UINavigationController here because I need the navbar that comes with it.我确实想在这里使用 UINavigationController 因为我需要它附带的导航栏。 Any suggestions greatly appreciated...任何建议都非常感谢...

When you say .present , the way the presented view controller occupies the screen is up to you.当您说.present时,呈现的视图 controller 占据屏幕的方式取决于您。 Make a custom transition and supply your own UIPresentationController subclass with an implementation of frameOfPresentedViewInContainerView to say where the presented view controller's view should go.进行自定义转换并为您自己的 UIPresentationController 子类提供frameOfPresentedViewInContainerView的实现,以说明呈现的视图控制器的视图应位于 go 的位置。

This is a silly example with a custom animation as well, but you don't have to customize the animation if you don't want to.这是一个自定义 animation 的愚蠢示例,但如果您不想自定义 animation,则不必自定义。 The important thing is that I've made the presented view controller smaller than it normally would be.重要的是,我使呈现的视图 controller 比正常情况小。

在此处输入图像描述

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

相关问题 有没有办法在 UINavigationController Swift Xcode 中更改 UINavigationBar 的高度? - Is there a way to change the height of a UINavigationBar in a UINavigationController Swift Xcode? UINavigationController工具栏高度在方向更改时 - UINavigationController toolbar height on orientation change 动画集合视图高度变化(快速) - Animating collection view height change (Swift) 更改UINavigationController的文本在Swift中不起作用 - Change the text of UINavigationController not working in Swift 如何快速更改集合视图的高度 - How to change the height of the collection view in swift 使视图的高度随图像Xcode的高度动态变化 - Make the height of the view dynamically change with height of image Xcode swift 快速更改UITabBar中“更多” UINavigationController的背景颜色 - Change the background color of “More” UINavigationController in UITabBar in swift Swift - UINavigationController栏颜色不会以编程方式更改 - Swift - UINavigationController bar color does not change programmatically iOS Swift 3-自动布局,更改高度取决于文本或视图大小 - iOS Swift 3 - Autolayout, change height depends on text or view size 使用 Swift 动画视图高度 - Animate view height with Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM