Reputation: 301
Guys I am using the swRevealViewController for my slide menu and I requires a bar button item to trigger the sliding of the menu. I have the navigation bar hidden.How can I do this with a normal button since it does not have a "target" and "action"
Open.target = self.revealViewController()
Open.action = Selector("revealToggle:")
Open is the bar button item.
Upvotes: 1
Views: 743
Reputation: 895
This is code by Mehul translated into Swift. I have not tested it, just translated:
override func viewWillAppear(animated: Bool) {
let imageView = UIImageView(image: UIImage(named: "UINavigationBarBackIndicatorDefault"))
imageView.tintColor = UIColor.redColor()
let label = UILabel.init()
label.textColor = UIColor.redColor()
label.text = "Blog"
label.sizeToFit()
let space = 6 as CGFloat
label.frame = CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+space, label.frame.origin.y, label.frame.size.width, label.frame.size.height)
let view = UIView(frame: 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.
let tap = UITapGestureRecognizer(target: self, action: "handleBack:")
view.addGestureRecognizer(tap)
view.userInteractionEnabled = true
UIView.animateWithDuration(0.33, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
label.alpha = 0
let 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
label.frame = orig
}, completion: nil)
let backButton = UIBarButtonItem(customView: view)
}
func handleBack(sender: AnyObject) {
}
Upvotes: 0
Reputation: 1689
Adding Custom barbutton item in 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
Upvotes: 0
Reputation: 7936
You can use the addTarget method of the UIButton like this:
yourButton.addTarget(self.revealViewController(), action: "revealToggle:", forControlEvents: .TouchUpInside)
Upvotes: 0
Reputation: 3178
You can create Your Own button and display it. simple.
The following code simulates the behaviour of the back button including animation.
-(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{
}
Upvotes: 1