2017-11-12 9 views
0

UITabBarControllerではなく、UITabBarを使用しています。ユーザーがタブをクリックするたびに、下線付きの画像を見ることができます。プログラムでタブバーアイテムを選択し、インジケータイメージを表示

public override func viewDidLoad() { 
    super.viewDidLoad() 
    UITabBar.appearance().selectionIndicatorImage = getImageWithColorPosition(color: UIColor.darkGray, size: CGSize(width:presenterView.frame.size.width/2, height:49), lineSize: CGSize(width:presenterView.frame.size.width/2, height:2)) 
} 

// getImageWithColorPositionは、ユーザーがタブ項目のいずれかをクリックしたときにのみ下線の画像が表示され、しかしUITabBarItem

func getImageWithColorPosition(color: UIColor, size: CGSize, lineSize: CGSize) -> UIImage { 
    let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) 
    let rectLine = CGRect(x: 0, y: size.height-lineSize.height, width: lineSize.width, height: lineSize.height) 
    UIGraphicsBeginImageContextWithOptions(size, false, 0) 
    UIColor.clear.setFill() 
    UIRectFill(rect) 
    color.setFill() 
    UIRectFill(rectLine) 
    let image = UIGraphicsGetImageFromCurrentImageContext()! 
    UIGraphicsEndImageContext() 
    return image 
} 

public override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    openItem1() 
} 

public func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { 
    if (item.tag == 0) { 
     openItem1() 
    } else { 
     openItem2() 
    } 
} 

の下に下線付きの画像を追加するためです。最初にユーザーが画面に表示されたときに、デフォルトのタブを選択したいと考えています。私はプログラムでそれをやっているので、下線の画像は表示されません。私はUITabBarでの選択はプログラムでは起動できないと言うスタックオーバーフローポストを読んでいます。

他の方法はありますか?

答えて

0

私が質問を正しく理解している場合は、ユーザーがアプリケーションを開くときに、UITabBarのタブをプログラムで選択する必要があります。これはAppDelegateの中で行うことができます。たとえば、didFinishLaunchingWithOptions

私のプロジェクトでは、特定の通知のようなイベントに応じてタブバーコントローラのタブにジャンプします。

if let tabbarController = self.window?.rootViewController as? UITabBarController { 
    tabbarController.selectedIndex = 0 
} 

PERがあればアプリは、コールドスタートと最後に開いたタブから起動された場合、インデックス0と1になり、選択した項目をデフォルト:AppDelegate内部Iは、プログラムタブを選択するには、次のコードを使用しますアプリは以前に起動され、アプリの切り替え機能で保持されていました。

これがあなたが探しているものです。

関連する問題