web-dev-qa-db-ja.com

迅速にタブバーアイテムに画像を設定する方法?

私はビューコントローラーを取り、それをナビゲーションコントローラーに埋め込みましたが、これもタブバーコントローラーに埋め込まれています。ストーリーボードを介して画像を設定しようとすると、画像がタブバーアイコンに表示されません。画像名は25です。

enter image description here

enter image description here

私に何ができるか教えてくださいプログラムでそれを行うにはどうすればよいですか?この目的のために適切な画像サイズを取得するにはどうすればよいですか?前もって感謝します。

12
KhanShaheb

MainTabbarViewController内

タブバーのアウトレットをバインドします:

@IBOutlet weak var myTabBar: UITabBar?

 override func viewDidLoad() {
      super.viewDidLoad()

      myTabBar?.tintColor = UIColor.white
      tabBarItem.title = ""

      setTabBarItems()

 }

ここで定義されているメソッドのタブバー項目を以下に設定します。

func setTabBarItems(){

      let myTabBarItem1 = (self.tabBar.items?[0])! as UITabBarItem
      myTabBarItem1.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem1.selectedImage = UIImage(named: "Selected ")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem1.title = ""
      myTabBarItem1.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

      let myTabBarItem2 = (self.tabBar.items?[1])! as UITabBarItem
      myTabBarItem2.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem2.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem2.title = ""
      myTabBarItem2.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)


      let myTabBarItem3 = (self.tabBar.items?[2])! as UITabBarItem
      myTabBarItem3.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem3.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem3.title = ""
      myTabBarItem3.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

      let myTabBarItem4 = (self.tabBar.items?[3])! as UITabBarItem
      myTabBarItem4.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem4.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem4.title = ""
      myTabBarItem4.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

 }

乾杯!

16

appDelegateクラスを追加します。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
    window=UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = setTabbar()
    self.window?.makeKeyAndVisible()
    window?.backgroundColor=UIColor.white
    return true
}

func setTabbar() -> UITabBarController
{
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let tabbarcntrl = UITabBarController()

    let Home = storyboard.instantiateViewController(withIdentifier: "HomeView") // 1st tab bar viewcontroller
    let Followed = storyboard.instantiateViewController(withIdentifier: "FollowedView") // 2nd tab bar viewcontroller
    let Message = storyboard.instantiateViewController(withIdentifier: "MessageView") // 3rd tab bar viewcontroller

    // all viewcontroller embedded navigationbar
    let nvHome = UINavigationController(rootViewController: Home)
    let nvFollowed = UINavigationController(rootViewController: Followed)
    let nvMessage = UINavigationController(rootViewController: Message)

    // all viewcontroller navigationbar hidden
    nvHome.setNavigationBarHidden(true, animated: false)
    nvFollowed.setNavigationBarHidden(true, animated: false)
    nvMessage.setNavigationBarHidden(true, animated: false)

    tabbarcntrl.viewControllers = [nvHome,nvFollowed,nvMessage]

    let tabbar = tabbarcntrl.tabBar
    tabbar.barTintColor = UIColor.black
    tabbar.backgroundColor = UIColor.black
    tabbar.tintColor = UIColor(red: 43/255, green: 180/255, blue: 0/255, alpha: 1)

    //UITabBar.appearance().tintColor = UIColor.white
    let attributes = [NSFontAttributeName:UIFont(name: "Montserrat-Light", size: 10)!,NSForegroundColorAttributeName:UIColor.white]
    let attributes1 = [NSFontAttributeName:UIFont(name: "Montserrat-Light", size: 10)!,NSForegroundColorAttributeName:UIColor(red: 43/255, green: 180/255, blue: 0/255, alpha: 1)]

    UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .normal)
    UITabBarItem.appearance().setTitleTextAttributes(attributes1, for: .selected)


    let tabHome = tabbar.items![0]
    tabHome.title = "Home" // tabbar titlee
    tabHome.image=UIImage(named: "icon_home.png")?.withRenderingMode(.alwaysOriginal) // deselect image
    tabHome.selectedImage = UIImage(named: "icon_home.png")?.withRenderingMode(.alwaysOriginal) // select image
    tabHome.titlePositionAdjustment.vertical = tabHome.titlePositionAdjustment.vertical-4 // title position change

    let tabFoll = tabbar.items![1]
    tabFoll.title = "Followed"
    tabFoll.image=UIImage(named: "icon_fold.png")?.withRenderingMode(.alwaysOriginal)
    tabFoll.selectedImage=UIImage(named: "icon_fold.png")?.withRenderingMode(.alwaysOriginal)
    tabFoll.titlePositionAdjustment.vertical = tabFoll.titlePositionAdjustment.vertical-4

    let tabMsg = tabbar.items![3]
    tabMsg.title = "Message"
    tabMsg.image=UIImage(named: "icon_mail.png")?.withRenderingMode(.alwaysOriginal)
    tabMsg.selectedImage=UIImage(named: "icon_mail.png")?.withRenderingMode(.alwaysOriginal)
    tabMsg.titlePositionAdjustment.vertical = tabMsg.titlePositionAdjustment.vertical-4

    return tabbarcntrl
}
10
Jayesh Miruliya

両方の画像を設定-選択/選択状態

See This

5
iDeveloper

すべてのことを正しい方法で行っていますが、唯一の問題は、tabbaritem画像のサイズが正しくないことです。tabbaritem画像の実際のサイズについては、この表を参照してください。

enter image description here

4
Sumit Dhariwal

Swift 4および5では、以下の拡張機能を使用できます。常に同じ数の画像、選択した画像、タイトルを渡すことを忘れないでください。ただし、タイトルを設定したくない場合は、タイトルにnilを渡してください。 。

拡張UITabBarController {

    func setUpImagaOntabbar(_ selectedImage : [UIImage], _ image : [UIImage], _ title : [String]?){

        for (index,vals) in image.enumerated(){

            if let tab = self.tabBar.items?[index]{

                tab.image = image[index]
                tab.image = selectedImage[index]
                if let tile = title[index]{
                   tab.title = title[index]
                  }

            }
        }
    }
}
0
Talha Rasool