ios - 如何使用普通按钮而不是条形按钮项目

标签 ios swift uiview uibutton

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

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

打开是条形按钮项。

最佳答案

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

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

-(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{
}

关于ios - 如何使用普通按钮而不是条形按钮项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33408230/

相关文章:

IOS 应用程序无法通过 socket.io(SIOSocket) 连接到 sails.js 服务器

ios - 使用 JIRA API 接收项目列表

ios - 如何获取对已实例化的 ViewController 的引用?

ios - swift 中的单元测试 | IOS Storyboard |无法转换 objective-c 中定义的 View Controller 类

ios - UIView subview 放置

ios - 快速自动布局+尺寸等级问题

ios - AppDelegate 的 'downcasting' 会失败吗?

objective-c - 其他类中的 UIApplicationDelegate 函数

iOS11 - Realm 框架未构建

ios - UIView Frame Size Didn't Change UIImageView 放在里面