Как создать UIButton для отображения того же эффекта, что и UIBarButtonItem? - PullRequest
3 голосов
/ 10 октября 2011

Я хочу создать UIButton, но такой же внешний вид UIBarButtonItem.Как это сделать ??

Ответы [ 5 ]

5 голосов
/ 08 марта 2012

нашел это полезным

https://github.com/0xced/UIKit-Artwork-Extractor

4 голосов
/ 10 октября 2011

За пределами панели навигации единственный способ создать кнопку с таким же стилем, как у UIBarButton, - это на самом деле создать необходимые фоновые изображения самостоятельно.Нет класса, который вы могли бы использовать для этого (очевидно, что есть класс, но он считается закрытым API, и его использование отклонит ваше приложение).

1 голос
/ 12 августа 2015

Вы можете сделать это, установив 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)
    }
}
0 голосов
/ 29 мая 2013

Возможно, немного поздно, но если кто-то сталкивается с этим, есть и другая возможность без использования изображений. Вместо этого я использую 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

Надеюсь, это кому-нибудь поможет

0 голосов
/ 08 марта 2012

Я нашел это через другой форум .. класс GradientButton.

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

...