如何使用普通按钮而不是条形按钮项
Posted
技术标签:
【中文标题】如何使用普通按钮而不是条形按钮项【英文标题】:How to use normal button instead of bar button item 【发布时间】:2015-10-29 07:27:54 【问题描述】:伙计们,我正在为我的滑动菜单使用 swRevealViewController,我需要一个条形按钮项来触发菜单的滑动。我隐藏了导航栏。如何使用普通按钮执行此操作,因为它没有“目标”和“操作”
Open.target = self.revealViewController()
Open.action = Selector("revealToggle:")
Open 是条形按钮项。
【问题讨论】:
【参考方案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
【讨论】:
对不起我的无知,我只知道 Swift 哦。对不起丹尼尔你必须搜索。我不知道斯威夫特。 非常感谢您的努力 谢谢。丹尼尔..继续编码..当你需要帮助时告诉我,我会在你身边。我的朋友【参考方案2】:你可以像这样使用 UIButton 的 addTarget 方法:
yourButton.addTarget(self.revealViewController(), action: "revealToggle:", forControlEvents: .TouchUpInside)
【讨论】:
【参考方案3】:在 SWRevealViewController 中添加自定义 barbutton 项
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
【讨论】:
【参考方案4】:这是 Mehul 翻译成 Swift 的代码。我没测试过,只是翻译一下:
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)
【讨论】:
以上是关于如何使用普通按钮而不是条形按钮项的主要内容,如果未能解决你的问题,请参考以下文章