Introduction
Welcome to the documentation for the GKNavigationBarSwift framework. This framework provides a powerful and customizable navigation bar for your iOS app, built entirely in Swift. With GKNavigationBarSwift, you can easily create and customize navigation bars that perfectly match your app’s style and branding.
Installation
To install GKNavigationBarSwift in your project, follow these steps:
- Open your Xcode project or workspace.
- Go to “File” > “Swift Packages” > “Add Package Dependency”.
- Enter the following URL:
https://github.com/GKarus/GKNavigationBarSwift.git
- Click “Next” and select the desired version of GKNavigationBarSwift.
- Click “Finish”. Xcode will download and integrate the package into your project.
Usage
Follow the steps below to use GKNavigationBarSwift in your project:
Step 1: Import the Framework
In the class where you want to use GKNavigationBarSwift, import the framework:
// Swift
import GKNavigationBarSwift
Step 2: Create a Navigation Bar
To create a GKNavigationBar, you can initialize it using the init()
method:
// Swift
let navigationBar = GKNavigationBar()
Step 3: Customize the Navigation Bar
GKNavigationBarSwift offers various customization options to tailor the navigation bar according to your app’s design:
Custom Bar Color
To set a custom background color for the navigation bar, use the barColor
property:
// Swift
navigationBar.barColor = UIColor.red
Custom Bar Title
To set a custom title for the navigation bar, use the title
property:
// Swift
navigationBar.title = "My App"
Customizing Title Text Attributes
You can also customize the appearance of the title text using the titleTextAttributes
property:
// Swift
navigationBar.titleTextAttributes = [
.foregroundColor: UIColor.white,
.font: UIFont.systemFont(ofSize: 17)
]
Adding Buttons
To add buttons to the navigation bar, you can use the leftBarButtonItem
and rightBarButtonItem
properties:
// Swift
let backButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(backButtonTapped))
navigationBar.leftBarButtonItem = backButton
Step 4: Display the Navigation Bar
Once you have created and customized the navigation bar, you can display it by adding it as a subview to your view:
// Swift
view.addSubview(navigationBar)
Methods
GKNavigationBarSwift provides the following methods:
func setBarColor(_ color: UIColor)
Sets the background color of the navigation bar.
func setBarTitle(_ title: String)
Sets the title of the navigation bar.
func setLeftBarButtonItem(_ item: UIBarButtonItem)
Sets the left bar button item of the navigation bar.
func setRightBarButtonItem(_ item: UIBarButtonItem)
Sets the right bar button item of the navigation bar.
Examples
Here are a few examples of how you can use GKNavigationBarSwift:
Example 1: Basic Usage
In this example, we create a basic navigation bar with a custom bar color:
// Swift
let navigationBar = GKNavigationBar()
navigationBar.barColor = UIColor.green
view.addSubview(navigationBar)
Example 2: Adding Buttons
In this example, we create a navigation bar with a back button on the left and a settings button on the right:
// Swift
let navigationBar = GKNavigationBar()
let backButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(backButtonTapped))
let settingsButton = UIBarButtonItem(image: UIImage(named: "settings"), style: .plain, target: self, action: #selector(settingsButtonTapped))
navigationBar.leftBarButtonItem = backButton
navigationBar.rightBarButtonItem = settingsButton
view.addSubview(navigationBar)
Example 3: Custom Title
In this example, we create a navigation bar with a custom title:
// Swift
let navigationBar = GKNavigationBar()
navigationBar.title = "Hello, World!"
navigationBar.titleTextAttributes = [
.foregroundColor: UIColor.white,
.font: UIFont.boldSystemFont(ofSize: 20)
]
view.addSubview(navigationBar)
Conclusion
Congratulations! You have successfully learned how to integrate and use GKNavigationBarSwift in your iOS app. With the powerful customization options provided by this framework, you can create stunning navigation bars that perfectly match your app’s design.