2016-04-25 5 views
2

iOSでUITabBarItemの色を変更しようとしています。私はスチュアートによってMVVMCrossと簡単な例を使用しています:TabBarの色を変更する

var viewControllers = new UIViewController[] 
{ 
    CreateTabFor("1", "home", FirstViewModel.Child1), 
    CreateTabFor("2", "twitter", FirstViewModel.Child2), 
    CreateTabFor("3", "favorites", FirstViewModel.Child3), 
}; 
ViewControllers = viewControllers; 
CustomizableViewControllers = new UIViewController[] { }; 
SelectedViewController = ViewControllers[0]; 


private int _createdSoFarCount = 0; 

private UIViewController CreateTabFor(string title, string imageName, IMvxViewModel viewModel) 
{ 
    var controller = new UINavigationController(); 
    var screen = this.CreateViewControllerFor(viewModel) as UIViewController; 
    SetTitleAndTabBarItem(screen, title, imageName); 
    controller.PushViewController(screen, false); 
    return controller; 
} 

private void SetTitleAndTabBarItem(UIViewController screen, string title, string imageName) 
{ 
    screen.Title = title; 
    screen.TabBarItem = new UITabBarItem(title, UIImage.FromBundle("Images/Tabs/" + imageName + ".png"), 
              _createdSoFarCount); 
    _createdSoFarCount++; 
} 

例のイメージがあります:色で enter image description here

答えて

1

あなたがイメージを変える意味ですかは?あなたはの外観を設定することができSelectedBackGroundTint用の更新このコンストラクタは、選択された状態https://developer.xamarin.com/api/constructor/UIKit.UITabBarItem.UITabBarItem/p/System.String/UIKit.UIImage/UIKit.UIImage/

var tabBarItem = new UITabBarItem (title, offImage, onImage); 

を変更するselectedImageimageパラメータを持っている

private void SetTitleAndTabBarItem(UIViewController screen, string title, string imageName) 
    { 
     screen.Title = title; 

     var offImage = UIImage.FromBundle (imageName); 
     offImage = offImage.ImageWithRenderingMode (UIImageRenderingMode.AlwaysOriginal); 
     offImage.AccessibilityLabel = title; 

     var onImage = UIImage.FromBundle (imageName + "selected"); 
     onImage = onImage.ImageWithRenderingMode (UIImageRenderingMode.AlwaysOriginal); 
     onImage.AccessibilityLabel = title; 

     var tabBarItem = new UITabBarItem (title, offImage, onImage); 
     tabBarItem.AccessibilityLabel = title; 

     screen.TabBarItem = tabBarItem; 

     _createdSoFarCount++; 
    } 

:あなたはこれを行うことができますすべてUITabBar、これは

です

UITabBar.Appearance.TintColor = UIColor.Magenta; 
:あなたはこれですべてUITabBar Sの外観を設定することができBACKGROUNDCOLOR

UITabBarItem.Appearance.SetTitleTextAttributes (new UITextAttributes() { 
     TextColor = UIColor.Red 
}, UIControlState.Normal); 

UITabBarItem.Appearance.SetTitleTextAttributes (new UITextAttributes() { 
     TextColor = UIColor.Blue 
}, UIControlState.Selected); 

用:

UITabBar.Appearance.SelectedImageTintColor = UIColor.Red; 

TEXTCOLORのためにあなたはこれですべてUITabBarItem Sの外観を設定することができます

+0

だから、私は背景としてイメージを使用する必要がありますか? 例があります:https://i.imgsafe.org/aa8cee7.png –

+0

背景色やテキスト色を変更したいですか? –

+0

背景色。 –

関連する問題