Objective-C

[objc] JASidePanelsのハンバーガーボタンをUIImageにカスタマイズする

More than 1 year has passed since last update.

JASidePanelController.m を継承したクラスで 下記2つのメソッドをオーバーライド

- (UIBarButtonItem *)leftButtonForCenterPanel
+ (UIImage *)defaultImage

↓こんな感じで任意の画像にカスタマイズします

/**
* [override] 
*/
- (UIBarButtonItem *)leftButtonForCenterPanel
{
    UIImage *humbergerImage = [[self class] defaultImage];

    //ボタンアクションがきくようにUIButton作成
    //参考 : @see http://akisute.com/2010/04/uibarbuttonitem.html
    UIButton *customHumbergerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, humbergerImage.size.width*0.5, humbergerImage.size.height*0.5)];
    [customHumbergerButton setBackgroundImage:humbergerImage forState:UIControlStateNormal];
    [customHumbergerButton addTarget:self action:@selector(toggleLeftPanel:) forControlEvents:UIControlEventTouchUpInside];

    //バーボタンアイテム作成
    UIBarButtonItem* humbergerItem = [[UIBarButtonItem alloc] initWithCustomView:customHumbergerButton];
    return humbergerItem;
}

/**
* [override] 
*/
+ (UIImage *)defaultImage
{
    static UIImage *defaultImage = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
    {
        //任意の画像
        defaultImage = [UIImage imageNamed:@"HeaderBtnHunberger.png"];
    });
    return defaultImage;
}