123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- //
- // UIBarButtonItem+SXCreate.m
- // UINavigation-SXFixSpace
- //
- // Created by charles on 2017/9/8.
- // Copyright © 2017年 None. All rights reserved.
- //
- #import "UIBarButtonItem+SXCreate.h"
- @implementation UIBarButtonItem (SXCreate)
- +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(UIImage *)image {
- return [self itemWithTarget:target action:action nomalImage:image higeLightedImage:nil imageEdgeInsets:UIEdgeInsetsZero alignment:NSTextAlignmentLeft];
- }
- +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(UIImage *)image imageEdgeInsets:(UIEdgeInsets)imageEdgeInsets {
- return [self itemWithTarget:target action:action nomalImage:image higeLightedImage:nil imageEdgeInsets:imageEdgeInsets alignment:NSTextAlignmentLeft];
- }
- +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(UIImage *)image alignment:(NSTextAlignment)alignment{
- return [self itemWithTarget:target action:action nomalImage:image higeLightedImage:nil imageEdgeInsets:UIEdgeInsetsZero alignment:alignment];
- }
- +(UIBarButtonItem *)itemWithTarget:(id)target
- action:(SEL)action
- nomalImage:(UIImage *)nomalImage
- higeLightedImage:(UIImage *)higeLightedImage
- imageEdgeInsets:(UIEdgeInsets)imageEdgeInsets
- alignment:(NSTextAlignment)alignment{
-
- UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
- [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
-
- // [button setImage:[nomalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
- // if (higeLightedImage) {
- // [button setImage:higeLightedImage forState:UIControlStateHighlighted];
- // }
- UIImageView *imgV = [[UIImageView alloc] initWithImage:[nomalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
- [button addSubview:imgV];
-
- [button sizeToFit];
- if (button.bounds.size.width < 44) {
- CGFloat width = 44 / button.bounds.size.height * button.bounds.size.width;
- button.bounds = CGRectMake(0, 0, width, 44);
- }
- if (button.bounds.size.height > 44) {
- CGFloat height = 44 / button.bounds.size.width * button.bounds.size.height;
- button.bounds = CGRectMake(0, 0, 44, height);
- }
- CGFloat x = 0.0f;
- switch (alignment) {
- case NSTextAlignmentLeft:
- x = 0.0f;
- break;
- case NSTextAlignmentCenter:
- x = (button.mj_w - nomalImage.size.width)/2.0f;
- break;
- case NSTextAlignmentRight:
- x = button.mj_w - nomalImage.size.width;
- break;
- default:
- break;
- }
- imgV.frame = CGRectMake(0, (button.mj_h - nomalImage.size.height) / 2.0f, nomalImage.size.width, nomalImage.size.height);
- button.imageEdgeInsets = imageEdgeInsets;
- [button setEnlargeEdge:20];
- return [[UIBarButtonItem alloc] initWithCustomView:button];
-
- }
- +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action title:(NSString *)title {
- return [self itemWithTarget:target action:action title:title font:nil titleColor:nil highlightedColor:nil titleEdgeInsets:UIEdgeInsetsZero];
- }
- +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action title:(NSString *)title titleEdgeInsets:(UIEdgeInsets)titleEdgeInsets {
- return [self itemWithTarget:target action:action title:title font:nil titleColor:nil highlightedColor:nil titleEdgeInsets:titleEdgeInsets];
- }
- +(UIBarButtonItem *)itemWithTarget:(id)target
- action:(SEL)action
- title:(NSString *)title
- font:(UIFont *)font
- titleColor:(UIColor *)titleColor
- highlightedColor:(UIColor *)highlightedColor
- titleEdgeInsets:(UIEdgeInsets)titleEdgeInsets {
-
- UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
- [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
-
- [button setTitle:title forState:UIControlStateNormal];
- button.titleLabel.font = font?font:[UIFont systemFontOfSize:15];
- [button setTitleColor:titleColor?titleColor:[UIColor blackColor] forState:UIControlStateNormal];
- [button setTitleColor:highlightedColor?highlightedColor:[UIColor blackColor] forState:UIControlStateHighlighted];
- [button sizeToFit];
- if (button.bounds.size.width < 44) {
- CGFloat width = 44 / button.bounds.size.height * button.bounds.size.width;
- button.bounds = CGRectMake(0, 0, width, 44);
- }
- if (button.bounds.size.height > 44) {
- CGFloat height = 44 / button.bounds.size.width * button.bounds.size.height;
- button.bounds = CGRectMake(0, 0, 44, height);
- }
- button.titleEdgeInsets = titleEdgeInsets;
- return [[UIBarButtonItem alloc] initWithCustomView:button];
- }
- +(UIBarButtonItem *)fixedSpaceWithWidth:(CGFloat)width {
-
- UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
- fixedSpace.width = width;
- return fixedSpace;
- }
- @end
|