1

I'm developping an application with Xamarin for iOS. I have a page that is filled with a UIStackView that I populate with an image and buttons.

Everything works fine, despite the fact that nothing is visible, but if I click where the buttons should appear, it executes the good behaviour of the button (opening another page).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Foundation;
using UIKit;
using CoreGraphics;
using Cirrious.FluentLayouts.Touch;

namespace SmartTransportNatif.iOS
{
    public class MainViewController : UIViewController
    {

        UIButton btn;
        JourneyViewController journey;
        Dictionary<string, string> favStations;
        UIStackView stkView;

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            View.BackgroundColor = UIColor.White;
            Title = "Accueil";

            journey = new JourneyViewController();

            MyClass pcl = new MyClass();
            favStations = pcl.getFavStations();

            var img = new UIImageView(View.Frame);
            img.Image = UIImage.FromFile("train.png");

            var bounds = UIScreen.MainScreen.Bounds;
            stkView = new UIStackView(bounds);
            stkView.Axis = UILayoutConstraintAxis.Vertical;
            stkView.Distribution = UIStackViewDistribution.FillEqually;
            stkView.Spacing = 5;

            stkView.AddArrangedSubview(img);

            View.AddSubview(stkView);


            foreach(KeyValuePair<string, string> entry in favStations)
            {
                addButton(entry.Key, entry.Value);
            }

        }


        void addButton(string name, string station)
        {
            var btn = UIButton.FromType(UIButtonType.System);
            btn.Frame = new CGRect(0, 0, 280, 44);
            btn.SetTitle(name, UIControlState.Normal);
            btn.BackgroundColor = new UIColor(33,150,234,255);
            btn.SetTitleColor(new UIColor(255,255,255,255), 
            UIControlState.Normal);

            btn.TouchUpInside += (sender, e) =>
            {
                this.NavigationController.PushViewController(journey, true);
            };

            stkView.AddArrangedSubview(btn);
        }

    }
}

Does someone see what is wrong or missing?

A. Silva
  • 629
  • 1
  • 6
  • 16
  • Have you tried setting your button text color to something other than white? – Will Decker Jun 27 '17 at 20:16
  • I just tried it right now, I have the text from the buttons visible, but the background color is not working, and the image not showing. – A. Silva Jun 27 '17 at 20:23
  • I think UIButtonType.System doesn't use a background color even if you supply one. Try UIButtonType.Custom. As far as the image not showing, do you have a train.png file in your Resources folder with the BuildAction set to BundleResource? – Will Decker Jun 27 '17 at 20:26
  • I hadn't the buildaction to "BundleResource". Now the image is displayed. For the backgound color, even for the custom type, it doesn't work. – A. Silva Jun 27 '17 at 20:44
  • For uicustomtype buttons, you need to use BackgroundImage rather than BackgroundColor. – Grisha Jun 28 '17 at 04:21

0 Answers0