【问题标题】:Why do my UIBarButtonItem's do an animated slide-in when presenting a view controller?为什么我的 UIBarButtonItem 在呈现视图控制器时会进行动画滑入?
【发布时间】:2014-06-10 22:19:28
【问题描述】:

在我的 iOS 应用程序中,我展示了一个视图控制器,其 modalTransitionStyle = UIModalTransitionStyleFlipHorizontal。当控制器出现时,其工具栏中的文本 UIBarButtonItem 会在控制器翻转到位时执行奇怪的幻灯片动画。

我不希望这个动画发生。如何预防?

编辑:感谢 Swift 的简洁性,这里有一个重现问题的完整示例:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window!.backgroundColor = UIColor.whiteColor()
        self.window!.makeKeyAndVisible()

        let vc = TestVC()

        self.window!.rootViewController = UINavigationController(rootViewController: vc)

        return true
    }
}

class PresentedVC: UIViewController {
    init() {
        super.init(nibName: nil, bundle: nil)
        toolbarItems = [UIBarButtonItem(title: "Foo", style: .Plain, target: nil, action: nil)]
        modalTransitionStyle = .FlipHorizontal
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        self.navigationController!.setToolbarHidden(false, animated: false)
    }
}

class TestVC: UITableViewController {
    override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        let cell = UITableViewCell(style: .Default, reuseIdentifier: "noId")
        cell.textLabel.text = "Foo"
        return cell
    }

    override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        let vc = PresentedVC()
        let nvc = UINavigationController(rootViewController: vc)

        self.presentViewController(nvc, animated: true, completion: nil)
    }
}

【问题讨论】:

  • 为什么需要将navigationBar.hidden设置为FALSE?默认不是false吗?
  • 不,默认是这样的。
  • 是的,我在考虑导航栏,而您正在使用工具栏。我没有在 viewWillAppear 中将 setToolbarHidden 设置为 False,而是尝试在 TestVC 的 didSelectRowAtIndexPath 方法中将其设置为 false。它在这里工作,动画是正常的。这对你有用吗?

标签: ios cocoa-touch animation swift uitoolbar


【解决方案1】:

我通过在viewDidLoad 中进行调用来解决此问题,以便在构造控制器时工具栏始终可见。然后我使用viewWillAppear:viewWillDisappear: 分别显示和隐藏控制器,这样导航控制器中的其他控制器就没有工具栏,如果他们不应该有的话。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多