如何使用普通按钮代替条形按钮项

时间:2015-10-29 07:27:55

标签: ios swift uiview uibutton

伙计我正在使用swRevealViewController作为我的幻灯片菜单,我需要一个条形按钮项来触发菜单的滑动。我隐藏了导航栏。如何使用普通按钮执行此操作,因为它没有“目标”和“操作”

Open.target = self.revealViewController()
Open.action = Selector("revealToggle:")

打开是条形按钮项目。

4 个答案:

答案 0 :(得分:1)

您可以创建自己的按钮并显示它。简单。

以下代码模拟后退按钮的行为,包括动画。

-(void)viewWillAppear:(BOOL)animated{
        UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UINavigationBarBackIndicatorDefault"]];
        [imageView setTintColor:[UIColor redColor]];
        UILabel *label=[[UILabel alloc] init];
        [label setTextColor:[UIColor redColor]];
        [label setText:@"Blog"];
        [label sizeToFit];

        int space=6;
        label.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+space, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, label.frame.size.width+imageView.frame.size.width+space, imageView.frame.size.height)];

        view.bounds=CGRectMake(view.bounds.origin.x+8, view.bounds.origin.y-1, view.bounds.size.width, view.bounds.size.height);
        [view addSubview:imageView];
        [view addSubview:label];

        //instead of adding the button, in my opinion the better approach is to use a gesture recognizer.

        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBack:)];
        [view addGestureRecognizer:tap];
        [view setUserInteractionEnabled:YES];

        [UIView animateWithDuration:0.33 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
            label.alpha = 0.0;
            CGRect orig=label.frame;
            label.frame=CGRectMake(label.frame.origin.x+25, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
            label.alpha = 1.0;
            label.frame=orig;
        } completion:nil];

        UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:view];
}

- (void) handleBack:(id)sender{
}

答案 1 :(得分:0)

您可以使用UIButton的addTarget方法,如下所示:

yourButton.addTarget(self.revealViewController(), action: "revealToggle:", forControlEvents: .TouchUpInside)

答案 2 :(得分:0)

在SWRevealViewController

中添加自定义条形图项
    let revealController = SWRevealViewController()
    let button = UIButton(type: UIButtonType.Custom)
    button.frame = CGRectMake(0, 0, 30, 30)
    button.setImage(UIImage(named: "menu-icon.png"), forState: UIControlState.Normal);
    button.addTarget(revealController, action: "revealToggle:", forControlEvents: UIControlEvents.TouchUpInside)

    let barButton = UIBarButtonItem(customView: button)
    self.navigationItem.leftBarButtonItem = barButton

答案 3 :(得分:0)

这是Mehul编译成Swift的代码。我没有测试过,只是翻译过:

#ifndef UNITTESTS
    #define STATIC
#else
    #define STATIC static
#endif