如何创建一个UIButton来呈现与UIBarButtonItem相同的效果?

发布于 2024-12-08 20:45:51 字数 54 浏览 0 评论 0原文

我想创建一个 UIButton,但与 UIBarButtonItem 的外观相同。怎么办呢??

I want to create a UIButton, but same appearence of a UIBarButtonItem. How to do it??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

樱娆 2024-12-15 20:45:52

在导航栏之外,创建与 UIBarButton 样式相同的按钮的唯一方法是自己实际创建必要的背景图像。没有任何类可供您用于此目的(显然有一个类,但它被视为私有 API,使用它将使您的应用程序被拒绝)。

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).

樱&纷飞 2024-12-15 20:45:52

您可以通过将按钮图像上的 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)
    }
}

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)
    }
}
撩起发的微风 2024-12-15 20:45:52

我通过另一个论坛找到了这个.. GradientButton 类。

http://code.google.com/p/iphonegradientbuttons/downloads/list

I found this via another forum .. a GradientButton class.

http://code.google.com/p/iphonegradientbuttons/downloads/list

独﹏钓一江月 2024-12-15 20:45:52

可能有点晚了,但万一有人遇到这个问题,还有另一种不使用图像的可能性。我用 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

希望对某人有帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文