如何在Xamarin iOS中更改背面“ <”的字体大小和颜色?

时间:2019-02-23 11:09:26

标签: xamarin xamarin.ios

是否可以在Xamarin.iOS中更改后退“ <”按钮的大小和颜色?我有以下代码,但唯一更改的部分是导航回文本:

var attribute = new UITextAttributes();
attribute.TextColor = UIColor.Red;
attribute.Font = UIFont.SystemFontOfSize(15.0f, UIFontWeight.Regular);
UIBarButtonItem.Appearance.SetTitleTextAttributes(attribute, UIControlState.Normal);
UIBarButtonItem.Appearance.SetTitleTextAttributes(attribute, UIControlState.Highlighted);

enter image description here

更新,这是新的NavigationPage渲染器:

public class NavigationPageRenderer : NavigationRenderer
{
   public override void ViewDidLoad()
   {
      base.ViewDidLoad();

      UIButton backBtn = UIButton.FromType(UIButtonType.Custom);
      var size = new CGSize(25,25);

      UIImage backImg = UIImage.FromBundle("back").ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
      var newBackImage = ScalingImageToSize(backImg, size);

      backBtn.SetImage(newBackImage, UIControlState.Normal);
      backBtn.SetTitle("Back", UIControlState.Normal);
      backBtn.SetImage(newBackImage, UIControlState.Highlighted);
      backBtn.SetTitle("Back", UIControlState.Highlighted);   

      backBtn.TouchUpInside += (sender, eve) => {
         this.NavigationController.PopViewController(false);
      };

      NavigationItem.LeftBarButtonItem = new UIBarButtonItem(backBtn);
      NavigationBar.TintColor = UIColor.Red;
   }

   public UIImage ScalingImageToSize(UIImage sourceImage, CGSize newSize)
   {
      // Your code here

   }
}

更多更新:

使用上面的代码,我在下面添加了图片,该图片看起来与普通的iOS后退按钮不同,以查看它是否真的收到了消息:

enter image description here

,发现上面的代码不会将“ <”图像更改为新的箭头图像。

2 个答案:

答案 0 :(得分:1)

原因:

backButton中的图标“ <”是图像,而不是文本。因此,即使您设置标题的样式,它也仍然无效。

解决方案:

如果要设置图标“ <”的样式,则可以在视图控制器中创建新的后退按钮

首先下载图标“ <”,并将其放入资产中。请参考以下代码。

UIButton backBtn = UIButton.FromType(UIButtonType.Custom);
backBtn.Frame = new CGRect(0, 0, 30, 30);

UIImage backImg = UIImage.FromBundle("back").ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);


backBtn.SetImage(backImg, UIControlState.Normal);
backBtn.SetTitle("Back", UIControlState.Normal);
backBtn.SetImage(backImg, UIControlState.Highlighted);
backBtn.SetTitle("Back", UIControlState.Highlighted);

backBtn.TouchUpInside+= (sender, eve) => {

  this.NavigationController.PopViewController(false);

};

this.NavigationItem.LeftBarButtonItem=new UIBarButtonItem(backBtn);
this.NavigationController.NavigationBar.TintColor = UIColor.Red;

这是icon,您可以下载它。

更新

如果要设置图像尺寸,请使用以下方法

/*
sourceImage:the iamge that you download
newSize the size you want ( for example 25*25)
*/
public UIImage ScalingImageToSize(UIImage sourceImage,CGSize newSize)
{

 if(UIScreen.MainScreen.Scale==2.0) //@2x iPhone 6 7 8 
  {
    UIGraphics.BeginImageContextWithOptions(newSize, false, 2.0f);
  }


 else if(UIScreen.MainScreen.Scale == 3.0) //@3x iPhone 6p 7p 8p...
  {
    UIGraphics.BeginImageContextWithOptions(newSize, false, 3.0f);
  }

 else
  {
    UIGraphics.BeginImageContext(newSize);
  }

 sourceImage.Draw(new CGRect(0, 0, newSize.Width, newSize.Height));

 UIImage newImage = UIGraphics.GetImageFromCurrentImageContext();

 UIGraphics.EndImageContext();

 return newImage;

} 

答案 1 :(得分:0)

您可以为此使用UINavigationBar色彩:

 UINavigationBar.Appearance.TintColor = UIColor.Red;