Lazy addition of UIButton to UIScrollView

nodoidnodoid GBMember, Beta ✭✭✭

Hi,

I've seen a few examples in ObjC that shows how to add UIButtons to a UIScrollView using lazy loading - unfortunately, I can't find any examples for Xam.iOS that doesn't use a UITableView.

I do have some test code for lazy loading images (below) but using this for the button doesn't work.

Is there a simple way to add a UIButton lazily to a UIScrollView? All buttons are vertically added

public class LazyUICreation
    {
        static Lazy<UIImage> ViewImage;

        public LazyUICreation(string filename)
        {
            ViewImage = new Lazy<UIImage>(() => UIImage.FromFile(filename).Scale(new SizeF(36, 36), AppDelegate.Self.Retina ? 2 : 1));
        }

        public UIButton CreateLongVeryThinButton(PointF where, string text1, string tag = "", bool makeBold = false)
        {
            var posn = new RectangleF(where.X, where.Y, AppDelegate.Self.ScreenX - where.X, 50);

            var theButton = new UIButton(UIButtonType.Custom)
            {
                Frame = posn,
                BackgroundColor = UIColor.LightGray,
                AccessibilityLabel = tag
            };
            theButton.Layer.CornerRadius = 10f;

            var image = new UIImageView(new RectangleF(4, 4, 36, 36))
            {
                Image = ViewImage.Value
            };

            var topLabel = new UILabel(new RectangleF(80, 12, 206, 25))
            {
                Text = text1,
                TextColor = UIColor.Black,
                Font = !makeBold ? UIFont.SystemFontOfSize(17f) : UIFont.BoldSystemFontOfSize(17f),
                AdjustsFontSizeToFitWidth = true,
                BackgroundColor = UIColor.LightGray
            };
            theButton.AddSubviews(new UIView[]{ image, topLabel });
            return theButton;
        }
    }

Thanks

Posts

  • adamkempadamkemp USInsider, Developer Group Leader mod

    What do you mean by "lazy loading"? When would it be loaded?

  • nodoidnodoid GBMember, Beta ✭✭✭

    First 8 buttons show then as and when they're ready, the rest of the UI is populated. Pretty much the same as lazy image loading on twitter or Facebook, but for an entire UIButton on a scrollview

  • adamkempadamkemp USInsider, Developer Group Leader mod

    And why aren't you using a UITableView? You're going to save yourself a ton of work if you just use the tools that already do that for you. Otherwise what you have to do is use the Scrolled event and do all the work of determining which views are visible, removing the ones that aren't, adding the ones that are, etc. It's a pain. I don't recommend it.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Oh, and UICollectionView is another choice if what you need is more advanced layout options. It also does virtualization for you.

  • nodoidnodoid GBMember, Beta ✭✭✭

    I'm not using a table view for a good reason (I did consider using it at one point, but dismissed it as it would lead to more problems than it would solve). I'll have a look at a collection view.

Sign In or Register to comment.