【问题标题】:implementing GTScrollNavigationBar in swift快速实现 GTScrollNavigationBar
【发布时间】:2024-01-09 11:09:01
【问题描述】:

我正在尝试在我的 swift 项目中使用 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
    }

在此行展开可选值时显示错误意外发现 nil (self.window!.rootViewController as! UINavigationController).viewControllers = [viewController!] 有人可以告诉我我做错了什么吗?

【问题讨论】:

    标签: ios objective-c iphone swift swift2


    【解决方案1】:

    您的代码应按如下方式翻译成 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()
    

    我使用navigationBar 而非GTScrollNavigationBar 对其进行了测试;它似乎正在工作。

    【讨论】: