我想创建一个UIButton,但是UIBarButtonItem的外观相同。怎么做??
答案 0 :(得分:5)
答案 1 :(得分:4)
在导航栏之外,创建与UIBarButton
具有相同样式的按钮的唯一方法是自己创建必要的背景图像。没有可用于此类的课程(显然有一个课程,但它被认为是私有API,使用它会让你的应用程序被拒绝)。
答案 2 :(得分:1)
您可以通过将按钮图像上的renderMode设置为UIImageRenderingModeAlwaysTemplate
来实现。这将使它继承其祖先视图的tintColor。
这里是如何在Swift中完成的(无论如何,对于一个控制状态):
if let i = myBtn?.imageForState(UIControlState.Normal) {
if i.renderingMode != UIImageRenderingMode.AlwaysTemplate {
let newImage = i.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
myBtn?.setImage(newImage, forState: UIControlState.Normal)
}
}
答案 3 :(得分:0)
我通过另一个论坛发现了这个..一个GradientButton类。
http://code.google.com/p/iphonegradientbuttons/downloads/list
答案 4 :(得分:0)
可能会有点晚,但万一有人来到这里,还有另一种可能性而不使用图像。我改用QuartzCore。
这就是我的所作所为:
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
希望它有助于某人