ios swift 移除标题栏,用UIView自定义标题栏

import UIKit



class ViewController: UIViewController {



    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor.red

        

        // 创建自定义标题栏

        let customTitleBar = UIView()

        customTitleBar.backgroundColor = UIColor.blue

        customTitleBar.translatesAutoresizingMaskIntoConstraints = false

        self.view.addSubview(customTitleBar)

        

        // 添加标题栏的约束

        NSLayoutConstraint.activate([

            customTitleBar.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),

            customTitleBar.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),

            customTitleBar.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),

            customTitleBar.heightAnchor.constraint(equalToConstant: 60)  // 设定标题栏高度

        ])

        

        // 在标题栏中添加标题标签

        let titleLabel = UILabel()

        titleLabel.text = "自定义标题"

        titleLabel.textColor = .white

        titleLabel.font = UIFont.boldSystemFont(ofSize: 20)

        titleLabel.translatesAutoresizingMaskIntoConstraints = false

        customTitleBar.addSubview(titleLabel)

        

        // 设置标题标签居中

        NSLayoutConstraint.activate([

            titleLabel.centerXAnchor.constraint(equalTo: customTitleBar.centerXAnchor),

            titleLabel.centerYAnchor.constraint(equalTo: customTitleBar.centerYAnchor)

        ])

    }

}
//
//  SceneDelegate.swift
//  study
//
//  Created by apple on 2024/5/21.
//

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)
        
        let viewController = ViewController() // 这里根据你的实际情况创建视图控制器
        window?.rootViewController = viewController
        
        window?.makeKeyAndVisible()
    }

    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }

    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.

        // Save changes in the application's managed object context when the application transitions to the background.
        (UIApplication.shared.delegate as? AppDelegate)?.saveContext()
    }


}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值