我正在处理一个带有SnapKit的Xcode项目,没有故事板. 我没有碰AppDelegate. 我在SceneDelegate上有这个代码

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(windowScene: windowScene)
        window?.makeKeyAndVisible()
        window?.root查看器控制器 = 选项卡栏控制器()
    }
}

项目的层次 struct 如下:

  • 首页(集团)
  1. 选项卡栏控制器
  2. 查看器控制器
  3. 首页Collection 视图单元格
  • ShopPage(集团) ...

  • 消息页面(集团) ...

  • ProfilePage(集团)

  1. Profile查看器控制器
  2. Settings查看器控制器

当点击settingsButton(在ProfileView控制器上)时,我想打开Settings查看器控制器.

这是我在选项卡栏控制器中的代码:

import UIKit

class 选项卡栏控制器: UI选项卡栏控制器 {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupTabs()
        selectedIndex = 3
        tabBar.backgroundColor = .systemBackground
        tabBar.tintColor = UIColor(red: 0, green: 155/255, blue: 247/255, alpha: 1)
    }
    
    //MARK: - Tab Setup
    private func setupTabs(){
        let home = self.createNav(title: "Home", image: UIImage(systemName: "house"), vc: 查看器控制器())
        let shop = self.createNav(title: "Shop", image: UIImage(systemName: "bag"), vc: Shop查看器控制器())
        let message = self.createNav(title: "Message", image: UIImage(systemName: "ellipsis.message"), vc: Message查看器控制器())
        let profile = self.createNav(title: "Profile", image: UIImage(systemName: "person"), vc: Profile查看器控制器())
        self.set查看器控制器s([home, shop, message, profile], animated: true)
    }
    
    private func createNav(title: String, image: UIImage?, vc: UI查看器控制器) -> UINavigationController{
        let nav = UINavigationController(root查看器控制器: vc)
        nav.tabBarItem.title = title
        nav.tabBarItem.image = image
        return nav
    }
}

以下是我在ProfileView控制器中的代码:

class Profile查看器控制器: UI查看器控制器 {
    private lazy var settingButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(systemName: "gearshape"), for: .normal)
        button.addTarget(self, action: #selector(openSettings), for: .touchUpInside)
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(red: 250/255, green: 250/255, blue: 250/255, alpha: 1)
        setupUI()
    }
    
    @objc private func openSettings() {
        let settings查看器控制器 = Settings查看器控制器()
        navigationController?.push查看器控制器(settings查看器控制器, animated: true)
    }
}

private extension Profile查看器控制器 {
    func setupUI() {
        setupViews()
        setupConstraints()
    }
    
    func setupViews() {
        view.addSubview(settingButton)
    }
    
    func setupConstraints() {
        settingButton.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
    }
}

但当我点击设置按钮时,没有任何react .

我try 像在场景代理中那样设置root查看器控制器: window?.root查看器控制器 = UINavigationController(root查看器控制器: 选项卡栏控制器())

此外,我还try 在此处将ProfileView控制器设置为root查看器控制器

我也try 了现在的方法:

@objc private func openSettings() {
    let settings查看器控制器 = Settings查看器控制器()
    present(settings查看器控制器, animated: true)
}

Edit

对不起,我以为这部分代码与我的问题无关,所以没有提供给您.我的代码是这样的:

class Profile查看器控制器: UI查看器控制器 {
    private lazy var topView: UIView = {
        let view = UIView()
        return view
    }()
    
    private lazy var topTitle: UILabel = {
        let label = UILabel()
        label.text = "Profile"
        label.font = UIFont.boldSystemFont(ofSize: 19)
        return label
    }()
    
    private lazy var settingButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(systemName: "gearshape"), for: .normal)
        button.addTarget(self, action: #selector(openSettings), for: .touchUpInside)
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    @objc private func openSettings() {
        print("button")
        let settings查看器控制器 = Settings查看器控制器()
        navigationController?.push查看器控制器(settings查看器控制器, animated: true)
    }


}

// MARK: - setting iui methods
private extension Profile查看器控制器 {
    func setupUI() {
        setupViews()
        setupConstraints()
        view.backgroundColor = .systemBackground
    }
    
    func setupViews() {
        view.addSubview(topView)
        topView.addSubview(topTitle)
        topView.addSubview(settingButton)
    }
    
    func setupConstraints() {
        topView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(50)
            make.leading.trailing.equalToSuperview()
            make.height.equalToSuperview().multipliedBy(0.08)
        }
        
        topTitle.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        settingButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-10)
            make.height.equalToSuperview().multipliedBy(0.4)
            make.width.equalToSuperview().multipliedBy(0.15)
            make.centerY.equalToSuperview()
        }
    }
}

但事实证明,问题出在俯视图中,但我不知道确切的原因.如果我的按钮在俯视图中,它不会被按下,但如果在外面,那么一切都很好.

推荐答案

你们的每个标签都有UINavigationController.

因此,您的"配置文件"选项卡有一个UINavigationController,根视图控制器为ProfileViewController

然后将topView添加为ProfileViewController视图的子视图,并将其约束到102的顶部--忽略安全区域,其中包括导航栏.

如果我修改您的代码以添加一些背景 colored颜色 :

override func viewDidLoad() {
    super.viewDidLoad()
    setupUI()
    
    topView.backgroundColor = .yellow
    navigationController?.navigationBar.backgroundColor = .green.withAlphaComponent(0.5)
}

它看起来是这样的:

enter image description here

您不能点击导航栏覆盖的按钮(或任何其他UI元素).

如果你想在导航栏上有一个"标题"和一个右侧按钮,可以用导航栏来管理……

class ProfileViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .systemBackground
        
        // don't call anything in your current setupUI() func
        //setupUI()
        
        self.title = "Profile"
        let b = UIBarButtonItem(image: UIImage(systemName: "gearshape"), style: .plain, target: self, action: #selector(openSettings))
        navigationItem.rightBarButtonItem = b
    }
    
    @objc private func openSettings() {
        print("button")
        let settingsViewController = SettingsViewController()
        navigationController?.pushViewController(settingsViewController, animated: true)
    }
    
}

现在,它看起来是这样的:

enter image description here

并轻击齿轮按钮will呼叫openSettings()

Ios相关问答推荐

Xcode15.3上的生成错误:调用的对象类型';facebook::flipper::SocketCertificateProvider';不是函数或函数指针

如何遮罩,动画和添加阴影到CAShapeLayer?

如果条件不满足,S从@ViewBuilder返回什么?

ios PWA从外部链接返回后降低了innerheight

在TVOS中访问SWIFT中的objc++函数时发送给类的无法识别的 Select 符

如何删除NavigationView中的默认透明标头?

Xcode 15模拟器用x86_64编译

清除/删除 NavigationController 中的多个 ViewController

当 .searchable 修饰符处于活动状态时,如何将变量设置为 false?

当 Swift 枚举具有 any existential 作为其关联值之一时,我如何使它符合 `Equatable`?

SwiftUI 图表 BarMark 将注释对齐到图表顶部

使用 Foundation 解压缩文件

过渡动画在 iOS16 中不起作用,但在 iOS15 中起作用

SwiftUI:如何为 ScrollView 制作可拉伸(灵活)的粘性标题?

如何设置imageView的圆角半径?

动画 UILabel 字体大小更改

在 Swift 中禁用向后滑动手势

如何缩小 UIImage 并使其同时变得清晰/锐利而不是模糊?

Swift 中的日期到毫秒和回溯到日期

如何在ios中生成UUID