我有一个UITabBarController
,有三个标签。当按下某一个时,我希望用户立即看到一个人视图控制器(ABPersonViewController
类的一个实例)。
我不想仅将presentViewController()
方法与人员视图控制器一起用作参数,因为当用户可以看到基础视图控制器时,这会导致延迟。提出了。
我也无法让视图控制器继承自ABPersonViewController
,因为它是由Apple设置的,因此它不能被子类化。有没有办法可以实现这个目标?
感谢JAL的回答:
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
let navViewController = viewController as! UINavigationController
// First, check to see if the view controller is the one you want to override
if let myViewController = navViewController.viewControllers[0] as? ThirdViewController {
let abpvc = ABPersonViewController()
abpvc.personViewDelegate = self
self.navigationController?.pushViewController(abpvc, animated: true)
return false
}
return true
}
答案 0 :(得分:0)
使用UITabBarDelegate
方法shouldSelectViewController
:
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
// First, check to see if the view controller is the one you want to override
if let myViewController = viewController as? ViewControllerToOverride {
// do something here, present a new view controller, etc.
return false // then return false to prevent the tab from switching
}
要选择标签栏项目(如果您正在使用图片),请更改UITabBarItem
&#39} image
或selectedImage
。由于您在选择标签时返回false,因此您可能需要更改image
属性而不是selectedImage
。
self.tabBar.items?.first?.image = UIImage(...)
答案 1 :(得分:0)
<强>答案强>
基本思想是将UITabBarController子类化并在选项卡栏上放置一个UIButton,覆盖原始选项卡区域并向该按钮添加一个动作,这将触发相关的函数调用。
因此tap事件实际上永远不会传递给tabBarController,而只传递给按钮。这样用户就永远不会“看到基础视图控制器”。
示例代码
示例Swift 1.2代码,它将按钮添加到UITabBarController的中心
func AddSampleButton(){
let SampleButton = UIButton()
SampleButton.setTranslatesAutoresizingMaskIntoConstraints(false)
SampleButton.setBackgroundImage(addImage, forState: UIControlState.Normal)
SampleButton.addConstraint(NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: self.view.frame.size.width/5))
SampleButton.addConstraint(NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50))
self.view.addSubview(addButton)
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: addButton, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: addButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
SampleButton.enabled = true
SampleButton.addTarget(self, action: "SampleAction", forControlEvents: UIControlEvents.TouchUpInside)
}
func SampleAction() {
let abpvc = ABPersonViewController()
abpvc.personViewDelegate = self
self.navigationController?.pushViewController(abpvc, animated: true)
}
<强>参考强>:
http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/
<强>通知强>
如果您没有看到该按钮,请注意UIViewContoller的生命周期,因为这可能是您错误的来源。