在外面的任何地方点击时如何隐藏侧边菜单
how to hide side menu when tapped anywhere outside
我想在用户点击外面的任何地方时隐藏侧边菜单。菜单出现了,但我找不到任何方法来退出它。关于 hidesOnTap 的任何建议也将不胜感激。我还使用 table 视图来显示侧面菜单内容,每当用户点击任何选项时,它都会被重定向到该页面。如何在不进行任何选择的情况下隐藏菜单。下面是代码
#import "MainMenuViewController.h"
#import "UIImageView+AFNetworking.h"
#import "ProfileViewController.h"
#import "ChatMenuHomeVC.h"
@interface MainMenuViewController ()
{
NSString *username;
NSString *profilestr;
}
@property (nonatomic, weak) IBOutlet UITableViewCell *cellMain;
@end
@implementation MainMenuViewController
@synthesize lblUserName,profilePic;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
//SideMenuToProfileView
arrOfOptions = [[NSMutableArray alloc]init];
[self addTitle:@"Home" imageName:@"icon-sidemenu-home.png" segueName:@"slidingToHome"];
[self addTitle:@"My Chat" imageName:@"icon-sidemenu-chat.png" segueName:@"ChatHomeVc"];
[self addTitle:@"Invite Friends" imageName:@"icon-sidemenu-invite.png" segueName:@""];
[self addTitle:@"Settings" imageName:@"icon-sidemenu-setting.png" segueName:@"slidingToSettings"];
[super viewDidLoad];
UITapGestureRecognizer *tapAction1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(profileVC:)];
tapAction1.numberOfTapsRequired = 1;
profilePic.userInteractionEnabled = YES;
[profilePic addGestureRecognizer:tapAction1];
username = [[NSUserDefaults standardUserDefaults]
stringForKey:@"UserName"];
profilestr = [[NSUserDefaults standardUserDefaults] stringForKey: @"UserProfilePic"];
self.lblUserName.text = username;
[profilePic setImageWithURL:[NSURL URLWithString:profilestr] placeholderImage:[UIImage imageNamed:@"user"]];
self.profilePic.layer.cornerRadius = self.profilePic.frame.size.width / 2;
self.profilePic.clipsToBounds = YES;
// Do any additional setup after loading the view.
}
-(void)addTitle:(NSString *)title imageName:(NSString *)imageName segueName:(NSString *)segue
{
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[dic setObject:title forKey:@"title"];
[dic setObject:imageName forKey:@"image"];
[dic setObject:segue forKey:@"segue"];
[arrOfOptions addObject:dic];
}
- (void)profileVC:(UITapGestureRecognizer *)tapGesture
{
[self performSegueWithIdentifier:@"SideMenuToProfileView" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"SideMenuToProfileView"])
{
ProfileViewController *destViewController = segue.destinationViewController;
destViewController.senderStr = @"fromUserSideMenu";
}
}
创建一个背景视图,其大小与设备的宽度和高度相同。将点击手势识别器添加到视图并在点击视图时隐藏侧边菜单。或者您也可以使用按钮来获得相同的功能,在按钮操作方法中编写代码。
如下操作
var backgroundView : UIView = UIView()
func createBackgroundView()
{
backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: ScreenSize.SCREEN_WIDTH, height: ScreenSize.SCREEN_HEIGHT))
backgroundView.tag = 10
backgroundView.isHidden = true
}
您可以在侧边菜单出现时取消隐藏背景视图
我想在用户点击外面的任何地方时隐藏侧边菜单。菜单出现了,但我找不到任何方法来退出它。关于 hidesOnTap 的任何建议也将不胜感激。我还使用 table 视图来显示侧面菜单内容,每当用户点击任何选项时,它都会被重定向到该页面。如何在不进行任何选择的情况下隐藏菜单。下面是代码
#import "MainMenuViewController.h"
#import "UIImageView+AFNetworking.h"
#import "ProfileViewController.h"
#import "ChatMenuHomeVC.h"
@interface MainMenuViewController ()
{
NSString *username;
NSString *profilestr;
}
@property (nonatomic, weak) IBOutlet UITableViewCell *cellMain;
@end
@implementation MainMenuViewController
@synthesize lblUserName,profilePic;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
//SideMenuToProfileView
arrOfOptions = [[NSMutableArray alloc]init];
[self addTitle:@"Home" imageName:@"icon-sidemenu-home.png" segueName:@"slidingToHome"];
[self addTitle:@"My Chat" imageName:@"icon-sidemenu-chat.png" segueName:@"ChatHomeVc"];
[self addTitle:@"Invite Friends" imageName:@"icon-sidemenu-invite.png" segueName:@""];
[self addTitle:@"Settings" imageName:@"icon-sidemenu-setting.png" segueName:@"slidingToSettings"];
[super viewDidLoad];
UITapGestureRecognizer *tapAction1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(profileVC:)];
tapAction1.numberOfTapsRequired = 1;
profilePic.userInteractionEnabled = YES;
[profilePic addGestureRecognizer:tapAction1];
username = [[NSUserDefaults standardUserDefaults]
stringForKey:@"UserName"];
profilestr = [[NSUserDefaults standardUserDefaults] stringForKey: @"UserProfilePic"];
self.lblUserName.text = username;
[profilePic setImageWithURL:[NSURL URLWithString:profilestr] placeholderImage:[UIImage imageNamed:@"user"]];
self.profilePic.layer.cornerRadius = self.profilePic.frame.size.width / 2;
self.profilePic.clipsToBounds = YES;
// Do any additional setup after loading the view.
}
-(void)addTitle:(NSString *)title imageName:(NSString *)imageName segueName:(NSString *)segue
{
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[dic setObject:title forKey:@"title"];
[dic setObject:imageName forKey:@"image"];
[dic setObject:segue forKey:@"segue"];
[arrOfOptions addObject:dic];
}
- (void)profileVC:(UITapGestureRecognizer *)tapGesture
{
[self performSegueWithIdentifier:@"SideMenuToProfileView" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"SideMenuToProfileView"])
{
ProfileViewController *destViewController = segue.destinationViewController;
destViewController.senderStr = @"fromUserSideMenu";
}
}
创建一个背景视图,其大小与设备的宽度和高度相同。将点击手势识别器添加到视图并在点击视图时隐藏侧边菜单。或者您也可以使用按钮来获得相同的功能,在按钮操作方法中编写代码。
如下操作
var backgroundView : UIView = UIView()
func createBackgroundView()
{
backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: ScreenSize.SCREEN_WIDTH, height: ScreenSize.SCREEN_HEIGHT))
backgroundView.tag = 10
backgroundView.isHidden = true
}
您可以在侧边菜单出现时取消隐藏背景视图