当侧面菜单打开/关闭时,swrevealviewcontrtoller更改切换按钮图像?

时间:2016-07-06 06:11:02

标签: ios objective-c swrevealviewcontroller

我在我的应用中使用SWRevealViewContrtoller滑出侧边菜单。哪个工作正常。但我有一个问题,如何在点击按钮或用户拖动侧边菜单时更改侧边栏按钮图像。我指的是link

提前致谢

4 个答案:

答案 0 :(得分:1)

尝试执行以下操作 -

方法1 (根据您的参考链接):

在您的homeVC.mSWRevealViewControllerDelegate

@interface homeVC ()<SWRevealViewControllerDelegate>并添加viewDidLoad()  self.revealViewController.delegate = self;

然后添加此方法 -

-(void)revealController:(SWRevealViewController *)revealController didMoveToPosition:(FrontViewPosition)position{

    //check position here

    if(position == FrontViewPositionLeft) {

        [self.sideBarButton setImage:[UIImage imageNamed:@"open.png"] forState:UIControlStateNormal]; //self.sideBarButton is your toggle button IBOutlet

    }
    else if(position == FrontViewPositionRight) {

        [self.sideBarButton setImage:[UIImage imageNamed:@"close.jpg"] forState:UIControlStateNormal];
    }

}

方法2:

sideVC.m import -

#import "homeVC.h"
#import "SWRevealViewController.h"

现在添加viewWillAppear()viewWillDisappear() -

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    homeVC *vc=(UserProfileViewController*)self.revealViewController.frontViewController;
    [vc.sideBarButton setImage:[UIImage imageNamed:@"close.jpg"] forState:UIControlStateNormal];

}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    homeVC *vc=(UserProfileViewController*)self.revealViewController.frontViewController;
    [vc.sideBarButton setImage:[UIImage imageNamed:@"open.jpg"] forState:UIControlStateNormal];

}
方法1 方法2 的区别在于,当sideVC完成动画时,方法1 ,即位置设置为{{1}通过单击按钮或拖动视图,可以比按钮图像更改{}或FrontViewPositionLeft。但是当FrontViewPositionRight开始显示按钮图像更改并且sideVC消失时方法2

答案 1 :(得分:0)

试试FRDLivelyButton。在此库中,您可以更改UIBarButton图像。

希望它有所帮助。

答案 2 :(得分:0)

添加<SWRevealViewControllerDelegate>

设置self.revealViewController.delegate = self;

- (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position{

    //change button image below.

    if(position == FrontViewPositionLeft) {

        //drawer close

    } else {

        //drawer open
    }
}

这对我有用。快乐的编码

答案 3 :(得分:0)

更新Swift 3.0

添加 SWRevealViewControllerDelegate
viewDidLoad()中添加此

self.revealViewController().delegate = self 

 func revealController(_ revealController: SWRevealViewController!, willMoveTo position: FrontViewPosition) {
    if position == FrontViewPosition.left {
        //drawer close
    }
    else {
       //drawer open
    }
}