使用swift在左上角的导航栏上添加徽标

时间:2016-07-19 07:12:19

标签: ios swift uinavigationcontroller imageview uinavigationbar

我想在导航栏的左上角添加徽标。我试着在设计时间,但没有为我工作。

Screenshot for design time

下面是我想要的截图。

Screenshot

以下是我尝试的代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        let imgLogo : UIImage = UIImage(named:"Logo")!
        let imgViewLogo : UIImageView = UIImageView(image: imgLogo)
        imgViewLogo.frame = CGRectMake(20, 2, 60, 60)                

        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        let objNavigationController = UINavigationController()

        objNavigationController.navigationBar.addSubview(imgViewLogo)

        let mainView = ViewController(nibName: nil, bundle: nil)
        objNavigationController.viewControllers = [mainView]
        self.window!.rootViewController = objNavigationController
        self.window?.makeKeyAndVisible()

        return true
    }

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:3)

你必须将它放在UIImageView

let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView