private IList<nfloat> CalculateWidths() { var flowLayout = ButtonBarView.CollectionViewLayout as UICollectionViewFlowLayout; var numberOfCells = ViewControllers.Count; var minimumCellWidths = new List<nfloat>(); nfloat collectionViewContentWidth = 0; foreach (var viewController in ViewControllers) { var childController = viewController as IndicatorInfoProvider; var indicatorInfo = childController.GetIndicatorInfo(this); switch (ButtonBarItemSpec.ButtonBarItemSpecType) { case ButtonBarItemSpecType.CellClass: { var width = ButtonBarItemSpec.Width(indicatorInfo); minimumCellWidths.Add(width); collectionViewContentWidth += width; } break; case ButtonBarItemSpecType.NibFile: { var width = ButtonBarItemSpec.Width(indicatorInfo); minimumCellWidths.Add(width); collectionViewContentWidth += width; } break; } } var cellSpacingTotal = (numberOfCells - 1) * flowLayout.MinimumLineSpacing; collectionViewContentWidth += cellSpacingTotal; var collectionViewAvailableVisibleWidth = ButtonBarView.Frame.Width - flowLayout.SectionInset.Left - flowLayout.SectionInset.Right; if (!Settings.Style.ButtonBarItemsShouldFillAvailiableWidth || collectionViewAvailableVisibleWidth < collectionViewContentWidth) { return minimumCellWidths; } else { var stretchedCellWidthIfAllEqual = (collectionViewAvailableVisibleWidth - cellSpacingTotal) / numberOfCells; var generalMinimumCellWidth = CalculateStretchedCellWidths(minimumCellWidths, stretchedCellWidthIfAllEqual, 0); var stretchedCellWidths = new List<nfloat>(); foreach (var minimumCellWidthValue in minimumCellWidths) { var cellWidth = (minimumCellWidthValue > generalMinimumCellWidth) ? minimumCellWidthValue : generalMinimumCellWidth; stretchedCellWidths.Add(cellWidth); } return stretchedCellWidths; } }
public override void ViewDidLoad() { base.ViewDidLoad(); ButtonBarItemSpec = ButtonBarItemSpec.NibFile( nibName: "ButtonCell", bundle: NSBundle.FromClass(new ObjCRuntime.Class(typeof(ButtonBarViewCell))), width: (childItemInfo) => { var label = new UILabel(); label.TranslatesAutoresizingMaskIntoConstraints = false; label.Font = this.Settings.Style.ButtonBarItemFont; label.Text = childItemInfo.Title; var labelSize = label.IntrinsicContentSize; return labelSize.Width + (this.Settings.Style.ButtonBarItemLeftRightMargin?? 8) * 2; }); UICollectionViewFlowLayout flowLayout; if (ButtonBarView == null) { flowLayout = new UICollectionViewFlowLayout(); flowLayout.ScrollDirection = UICollectionViewScrollDirection.Horizontal; var buttonBarHeight = Settings.Style.ButtonBarHeight ?? 44f; var buttonBar = new ButtonBarView(new CGRect(0, 0, View.Frame.Width, buttonBarHeight), flowLayout); buttonBar.BackgroundColor = UIColor.Orange; buttonBar.SelectedBar.BackgroundColor = UIColor.Black; buttonBar.AutoresizingMask = UIViewAutoresizing.FlexibleWidth; var newContainerViewFrame = ContainerView.Frame; newContainerViewFrame.Y = buttonBarHeight; newContainerViewFrame.Height = ContainerView.Frame.Height - (buttonBarHeight - ContainerView.Frame.Y); ContainerView.Frame = newContainerViewFrame; ButtonBarView = buttonBar; } if (ButtonBarView.Superview == null) { View.AddSubview(ButtonBarView); } if (ButtonBarView.Delegate == null) { ButtonBarView.Delegate = this; } if (ButtonBarView.DataSource == null) { ButtonBarView.DataSource = this; } ButtonBarView.ScrollsToTop = false; flowLayout = ButtonBarView.CollectionViewLayout as UICollectionViewFlowLayout; flowLayout.ScrollDirection = UICollectionViewScrollDirection.Horizontal; flowLayout.MinimumInteritemSpacing = 0; flowLayout.MinimumLineSpacing = Settings.Style.ButtonBarMinimumLineSpacing ?? flowLayout.MinimumLineSpacing; var sectionInset = flowLayout.SectionInset; flowLayout.SectionInset = new UIEdgeInsets(sectionInset.Top, Settings.Style.ButtonBarLeftContentInset ?? sectionInset.Left, sectionInset.Bottom, Settings.Style.ButtonBarRightContentInset ?? sectionInset.Right); ButtonBarView.ShowsHorizontalScrollIndicator = false; ButtonBarView.BackgroundColor = Settings.Style.ButtonBarBackgroundColor ?? ButtonBarView.BackgroundColor; ButtonBarView.SelectedBar.BackgroundColor = Settings.Style.SelectedBarBackgroundColor; ButtonBarView.SelectedBarHeight = Settings.Style.SelectedBarHeight; switch (ButtonBarItemSpec.ButtonBarItemSpecType) { case ButtonBarItemSpecType.NibFile: { ButtonBarView.RegisterNibForCell(UINib.FromName(ButtonBarItemSpec.NibName, ButtonBarItemSpec.Bundle), "Cell"); } break; case ButtonBarItemSpecType.CellClass: { ButtonBarView.RegisterClassForCell(typeof(ButtonBarViewCell), "Cell"); } break; } }