Overview
The shBarButtonItemBlocks library is a collection of UIBarButtonItem subclasses that allow you to easily add blocks of code as actions to your bar button items in iOS apps. This eliminates the need to write separate selector methods for each action, resulting in cleaner and more concise code.
Installation
To install shBarButtonItemBlocks in your project, you can use CocoaPods. Simply add the following line to your Podfile
:
pod 'shBarButtonItemBlocks'
Then, run the command pod install
in the Terminal.
Usage
Creating an Action Block
The main feature of shBarButtonItemBlocks is the ability to add blocks of code as actions to your bar button items.
To create an action block, you can use the provided UIBarButtonItem+SHBarButtonItemBlocks
category:
UIBarButtonItem *actionButton = [[UIBarButtonItem alloc] initWithTitle:@"Action" style:UIBarButtonItemStylePlain target:nil action:nil];
[actionButton setBlock:^(id sender) {
// Your code here
NSLog(@"Action button tapped");
}];
Adding Bar Button Items
To add the custom bar button items to your view controller’s navigation bar, you can use the leftBarButtonItemBlocks
and rightBarButtonItemBlocks
properties:
self.navigationItem.leftBarButtonItemBlocks = @[actionButton];
self.navigationItem.rightBarButtonItemBlocks = @[actionButton];
Compatibility
The shBarButtonItemBlocks library requires a minimum deployment target of iOS 8.0.
License
The shBarButtonItemBlocks library is released under the MIT license. See the LICENSE file for more details.
Conclusion
The shBarButtonItemBlocks library provides a convenient way to add blocks of code as actions to your bar button items in iOS apps. With its simple usage and compatibility with modern iOS versions, it can greatly improve the efficiency and readability of your code.