ios - 在 swift 中实现 GTScrollNavigationBar

标签 ios objective-c iphone swift swift2

我正在尝试使用 GTScrollNavigationBar在我的快速项目中。我是 swift 和 iOS 的新手。所以我正在使用 obj-C 中的演示项目来实现它。 演示项目的 AppDelegate 如下所示

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    self.viewController = [[DemoTableViewController alloc] initWithStyle:UITableViewStylePlain];
    self.navController = [[UINavigationController alloc] initWithNavigationBarClass:[GTScrollNavigationBar class] toolbarClass:nil];
    [self.navController setViewControllers:@[self.viewController] animated:NO];
    self.window.rootViewController = self.navController;
    [self.window makeKeyAndVisible];
    return YES;
}

我已经用swift实现了,看起来像这样

var window: UIWindow?
    var navigationController : UINavigationController?
    var viewController : UIViewController?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window?.backgroundColor = UIColor.whiteColor()

        self.viewController = TableViewController(style: UITableViewStyle.Plain)
        self.navigationController = UINavigationController(navigationBarClass: GTScrollNavigationBar.self, toolbarClass: nil)
        (self.window!.rootViewController as! UINavigationController).viewControllers = [viewController!]
        self.window?.rootViewController = self.navigationController
        self.window?.makeKeyAndVisible()


        return true
    }

它显示错误 unwrappedly found nil while unwrapping an Optional value at this line (self.window!.rootViewController as! UINavigationController).viewControllers = [viewController!] 有人可以告诉我我做错了什么吗?

最佳答案

您的代码应按如下方式转换为 Swift:

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()

self.viewController = UITableViewController(style: UITableViewStyle.Plain)

self.navigationController = UINavigationController(navigationBarClass: GTScrollNavigationBar.self, toolbarClass: nil)
self.navigationController?.viewControllers = [viewController!]

self.window?.rootViewController = self.navigationController
self.window?.makeKeyAndVisible()

我使用 GTScrollNavigationBar 以外的 navigationBar 对其进行了测试;它似乎在工作。

关于ios - 在 swift 中实现 GTScrollNavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32153794/

相关文章:

iphone - URL 主机名返回 null

ios - UITableViewControllers 上的 prepareforSegue 方法

ios swift facebook登录fb_mobile_login_native_app_switch_dialog_result错误

ios - 在编辑模式下滑动删除不起作用

objective-c - 在 Objective-C 中是否需要一个类来覆盖 +alloc?

iphone - MKMapView 中显示多个注释标注

iPhone 用户界面设计

iOS 扩展 - 致命异常 : com. firebase.core 默认应用已配置

ios - 堆栈 View 中堆栈 View 的约束

iphone - 事件指示器无法正常工作