Display Button With Text & Image In Xamarin IOS - xamarin

I want to design a custom button in Xamarin IOS to display both Text and Image.
Please find the below image.
You can find the Text and down arrow symbol in the above image.
the down arrow symbol should always display after the text. The button text is dynamic.
Can any one suggest me to solve this.

You want to use the TitleEdgeInsets & ImageEdgeInsets properties of the UIButton to control where the inset of the text and image begins:
button.TitleEdgeInsets = new UIEdgeInsets(0, -button.ImageView.Frame.Size.Width, 0, button.ImageView.Frame.Size.Width);
button.ImageEdgeInsets = new UIEdgeInsets(0, button.TitleLabel.Frame.Size.Width, 0, -button.TitleLabel.Frame.Size.Width);
Full Example:
button = new UIButton(UIButtonType.System);
button.Frame = new CGRect(150, 20, 200, 50);
button.Layer.CornerRadius = 5;
button.BackgroundColor = UIColor.Black;
button.SetTitleColor(UIColor.White, UIControlState.Normal);
button.SetImage(UIImage.FromFile("ratingstar.png"), UIControlState.Normal);
button.SetTitle("StackOverflow", UIControlState.Normal);
button.TitleEdgeInsets = new UIEdgeInsets(0, -button.ImageView.Frame.Size.Width, 0, button.ImageView.Frame.Size.Width);
button.ImageEdgeInsets = new UIEdgeInsets(0, button.TitleLabel.Frame.Size.Width, 0, -button.TitleLabel.Frame.Size.Width);

Related

How to configure the zise tag when the device screen size is modified

In xamarin I would like a label text with a maximum length. When they modify the size of the device screen, all the labels are not displayed correctly
I am using xamarin forms 5.0.0.2244
var labelCountry = new Label
{
Text = $"Bienvenido a {Settings.Country} ",
FontSize = 14,
TextColor = Color.Blue
};
read the docs on Layout Options
HorizontalOptions = LayoutOptions.FillAndExpand
this will make the Label expand to fill the size of it's parent container

how to add logo to navigation bar in ios xamarin forms

In my project I want to add logo to navigation bar. In android by using custom renderer I have placed the logo to navigation bar. But i don't know how to set the logo in navigation bar at globally in ios xamarin forms. Anyone give suggestion how to solve this.
I have tried like this,
NavigationPage.SetTilteIcon(this, "icon.png"); // But image is not display
Icon placement Reference Screenshot
You can try by setting the icon as:
Application.Current.MainPage = new NavigationPage(new FirstPageName() { Icon="icon.png"});
While navigating to another page you can write this as:
this.Navigation.PushAsync(new SecondPageName() { Icon="icon.png"});
Make sure you have the icon image in iOS project folder.
You can try custom renderer for a single page. But we can't set it at globally. Because in iOS every ViewController has a NavigationItem, we should set it when we need.
Here is some code about how to set NavigationItem in page renderer:
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
UIView leftView = new UIView(new CGRect(0, 0, 100, 40));
UIImageView imgView = new UIImageView(UIImage.FromBundle("icon.png"));
imgView.Frame = new CGRect(0, 5, 30, 30);
leftView.AddSubview(imgView);
UILabel label = new UILabel(new CGRect(30, 10, 70, 20));
label.Text = "page Title";
leftView.AddSubview(label);
//add event
UITapGestureRecognizer tap = new UITapGestureRecognizer((sender) =>
{
});
leftView.AddGestureRecognizer(tap);
leftItem = new UIBarButtonItem(leftView);
NavigationController.TopViewController.NavigationItem.SetLeftBarButtonItem(leftItem, true);
}
Please notice that put this image source in iOS's project.
Also if you do want to show this icon image in each page. Try to create a base page and construct this renderer for it. Then let your page inherit from it.

Aligning a Frame in Xamarin Forms

How to modify the height of the Frame layout. I have tried to set the HeightRequest for the frame, but it doesnt work. Please check the below and attached image.
Frame
Frame frame = new Frame()
{
HasShadow = false,
Margin = new Thickness(10, 0, 0, 10),
BackgroundColor = Color.FromHex("#CDD0D1")
};
Label
Label tag = new Label()
{
TextColor = Color.Black,
FontSize = 14,
};
frame.Content = tag;
Expected Output: My excepted output should be rectangle frame instead of square.
Mainly,the frame control in Xamarin is take there own padding in all directions so add padding 0 of frame as:
Frame frame=new Frame{Padding=new Thickness(0)};

Xamarin - Gradient background

I'm trying to programmatically set a background gradient like this in Xamarin iOS:
CGColor[] colors = new CGColor[] { new UIColor(red: 0.05f, green:0.44f, blue:0.73f, alpha:1.0f).CGColor,
new UIColor(red: 45/255, green: 128 / 255, blue: 153 / 255, alpha: 0.01f ).CGColor};
CAGradientLayer gradientLayer = new CAGradientLayer();
gradientLayer.Frame = this.View.Bounds;
gradientLayer.Colors = colors;
this.View.Layer.AddSublayer(gradientLayer);
And it works, but I have form elements on the screen (labels, buttons, etc) and this is causing all of them to appear faded, as if the layer appears directly on top of the elements. I think this is what the code is doing - it's rendering the entire page, and then adding the layer with opacity on top of the screen (I want it behind it instead).
What I'd like, instead, is to have the background set as gradient, but any elements I add will not be faded and try to match the background colors.
I tried adding a view on top of the standard view, and setting the alpha to 1, background to white, but that didn't seem to help (suggesting my theory that programmatically it's adding a layer on top of everything after it's rendered).
What am I doing wrong?
Thanks!
I know I am late for the show but for future reference, I use:
this.View.Layer.InsertSublayer(gradientLayer, 0);
The 0 represents index.
Your gradient layer that is assign to the root View of the ViewController is being displayed "behind" all the other Views.
By default, views (UILabel, UIButton, etc) do not have a background color set and thus will blend with that background layer, if that is not the effect that you are looking for assign a background to them:
var button = new UIButton(UIButtonType.System);
button.Frame = new CGRect(40, 300, 120, 40);
button.SetTitle("No Background", UIControlState.Normal);
button.TintColor = UIColor.Red;
View.AddSubview(button);
var button2 = new UIButton(UIButtonType.System);
button2.Frame = new CGRect(180, 300, 100, 40);
button2.SetTitle("Background", UIControlState.Normal);
button2.TintColor = UIColor.Red;
button2.BackgroundColor = UIColor.White;
View.AddSubview(button2);

Show difference image for PageOrientation.LandscapeRight by clicking the emulator button

The page starts as "Partrait" and I have code to catch the orientation change for adjust the user controls. I don't figure out why showing difference on the same code by changing the order to press the button on emulator to roated the screen. The following is my code to catch the orienation change event.
if (previousOrientation == PageOrientation.PortraitUp && e.Orientation == PageOrientation.LandscapeLeft)
{
this.LayoutRoot.Width = 730;
lst.lst.Height = 220;
lst.lst.Width = 730;
this.LayoutRoot.Margin = new Thickness(0, 0, 0, 0);
this.TitlePanel.Margin = new Thickness(0, 0, 65, 0);
}
if (e.Orientation.ToString().Contains("Portrait"))
{
this.LayoutRoot.Width = 480;
this.LayoutRoot.Margin = new Thickness(0, 0, 0, 0);
lst.lst.Height = 400;
lst.lst.Width = 450;
this.TitlePanel.Margin = new Thickness(12, 0, 40, 0);
this.TitlePanel.Margin = new Thickness(12, 17, 2, 0);
}
when I pressed second time on the first button on the emulator to rotate the screen, (previousOrientation == PageOrientation.PortraitUp && e.Orientation == PageOrientation.LandscapeRight) is fired. It shows the big application bar's background yellow color.
If I pressed first time on the second button on the emulator to rotate the screen, the same line "(previousOrientation == PageOrientation.PortraitUp && e.Orientation == PageOrientation.LandscapeRight)" is fired. It shows the narrow application bar's background yellow color that I wanted.
Since I am a new user, I cannot upload the image. If you are interested on my question, I can send the image to you. Thanks in advance.
I serached the forums # the following website. It seems it is the bug.
Application Bar resizes after rotation

Resources