gpt4 book ai didi

ios - subview Controller 大小问题

转载 作者:行者123 更新时间:2023-11-28 05:46:40 27 4
gpt4 key购买 nike

我有一个 View Controller ,导航栏中有一个分段控件,用于切换 subview Controller 与父 Controller 的连接。它工作正常,但我作为子 vc 拥有的 TableView 大小不正确。它在底部被切断。

母风投:

import UIKit

class ShareTabViewController: UIViewController {

let segementedControl = UISegmentedControl(items: ["Share", "Blog", "Progress"])

override func viewDidLoad() {
super.viewDidLoad()

self.segementedControl.sizeToFit()
self.segementedControl.tintColor = Constants.Colors.raPurple
self.segementedControl.selectedSegmentIndex = 0
self.segementedControl.addTarget(self, action: #selector(selectionChanged(_:)), for: .valueChanged)
self.navigationItem.titleView = self.segementedControl

let shareVC = ShareTableViewController()
self.view.addSubview(shareVC.view)
self.addChild(shareVC)
shareVC.didMove(toParent: self)
}

@objc func selectionChanged(_ sender: UISegmentedControl) {
for subview in self.view.subviews {
subview.removeFromSuperview()
}

for childVC in self.children {
childVC.removeFromParent()
}

switch sender.selectedSegmentIndex {
case 0:
let shareVC = ShareTableViewController()
self.view.addSubview(shareVC.view)
self.addChild(shareVC)
case 1:
let blogVC = BlogViewController()
self.view.addSubview(blogVC.view)
self.addChild(blogVC)
case 2:
let progressVC = ProgressViewController()
self.view.addSubview(progressVC.view)
self.addChild(progressVC)
default:
print("default")
}
}
}

enter image description here

最佳答案

您需要为 selectionChanged 中的添加和 viewDidLayoutSubviews 中的添加设置框架,因为它有错误的框架,或一般使用

NSLayoutConstraint.activate([ 
shareVC.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
shareVC.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
shareVC.view.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
shareVC.view.bottomAnchor.constraint(equalTo:self.view.safeAreaLayoutGuide.bottomAnchor)
])

关于ios - subview Controller 大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54426444/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com