I want to create a UIBu开发者_JS百科tton, but same appearence of a UIBarButtonItem. How to do it??
Found this useful
https://github.com/0xced/UIKit-Artwork-Extractor
Outside of a navigation bar, the only way to create a button with the same style as a UIBarButton
is to actually create the necessary background images yourself. There is no class that you may use for this (there is a class, obviously, but it's considered private API and using it will get your app rejected).
You can do it by setting the renderMode on the button's image to UIImageRenderingModeAlwaysTemplate
. That will make it inherit the tintColor of its ancestor view.
Here's how to do it in Swift (for one control state, anyway):
if let i = myBtn?.imageForState(UIControlState.Normal) {
if i.renderingMode != UIImageRenderingMode.AlwaysTemplate {
let newImage = i.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
myBtn?.setImage(newImage, forState: UIControlState.Normal)
}
}
I found this via another forum .. a GradientButton class.
http://code.google.com/p/iphonegradientbuttons/downloads/list
Might be a little late but in case someone comes accross this, there is also another possiblity without using images. I use QuartzCore instead.
This is what i do:
MyUIButton.h
#import <QuartzCore/QuartzCore.h>
@interface MyUIButton : UIView { //i think you can also subclass from UIButton, but i use a view to be more flexible
UIButton *_myInnerButton;
}
- (void) addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
- (void) removeTarget:(id) target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
@end
MyUIButton.m
#import "MyUIButton.h"
@implementation MyUIButton
- (void) dealloc {
if (_myInnerButton != nil) {
[_myInnerbutton removeFromSuperview];
_myInnerbutton = nil;
}
[super dealloc];
}
- (id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.clipsToBounds = YES;
_myInnerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:_myInnerButton];
CGRect innerButtonRect = CGRectZero;
innerButtonRect.size = self.frame.size;
_myInnerButton.frame = innerButtonRect;
CGRect iOSLookalikeViewRect = innerButtonRect;
iOSLookalikeViewRect.origin.height = -innerButtonRect.size.height/2;
UIView *iOSButtonView = [[UIView alloc] initWithFrame:iOSLookalikeViewRect];
iOSButtonView.layer.cornerRadius = 3;
[self insertSubview:iOSButtonView belowSubview:_myInnerButton];
UIColor *whiteColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.3];
UIColor *whiteColor2 = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];
[self setGradientColors:[NSArray arrayWithObjects:whiteColor.CGColor, whiteColor2.CGColor, nil] forView:iOSButtonView];
[iOSButtonView release];
}
return self;
}
- (void) setGradientColors:(NSArray*) gradientColors forView:(UIView*) view {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = gradientColors;
gradient.cornerRadius = view.layer.cornerRadius;
[view.layer insertSublayer:gradient atIndex:1];
[view.layer setRasterizationScale:[UIScreen mainScreen].scale]; //Optional for performance issues, can be removed also
}
//Implementation of the public methods not included, but just asign the methods to the _myInnerButton
@end
Hope it helps someone
精彩评论