【问题标题】:Xamarin and XCode: Resize UIView and UITableViewXamarin 和 XCode:调整 UIView 和 UITableView 的大小
【发布时间】:2017-09-14 08:32:39
【问题描述】:

我的屏幕很长(很多字段)。 XCode 中的结构:

View
  ScrollView
    View
       View
       .
       .
       .
       TableContainer (View)
         TableView

TableContainerTableView 的高度为 0。 当我进入页面时 - 我根据项目数设置 TableView 高度并根据项目数增加 TableView 高度。 一切正常。 但是,当我旋转设备时 - TableView 消失(高度重置为 0)。

类:

public partial class TrackingView: BaseView<TrackingViewModel>
    {
        public TrackingView() : base("TrackingView")
        {
        }

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

            var source = new RetrieveShipmentDetailsViewSource(PortsTable, ShipmentDetailsCell.Key, ShipmentDetailsCell.Key);
            PortsTable.Source = source;

            var set = this.CreateBindingSet<TrackingModel, TrackingViewModel>();

            set.Bind(ShipmentLabel).To(vm => vm.ShipmentNumber);
            set.Bind(CustCodeLabel).To(vm => vm.Shipment.CustomerCode);
            set.Bind(CustNameLabel).To(vm => vm.Shipment.CustomerName);
        );

            ViewModel.ShipmentLoadedSuccess += (sender, e) =>
            {
                InvokeOnMainThread(delegate
                {
                    PortsTable.ReloadData();
                    UpdateView();
                });
            };

            PortsTable.SeparatorStyle = UITableViewCellSeparatorStyle.None;
            PortsTable.ReloadData();

            set.Apply();            
        }

        private void UpdateView()
        {
            if (ViewModel.Shipment != null && ViewModel.Shipment.VesselPorts != null)
            {
                PortsTable.ContentSize = new CGSize(MainView.ContentSize.Width, (ViewModel.Shipment.VesselPorts.Count *  200) + 55);
                PortsTable.Frame = new CGRect(PortsTable.Frame.X, PortsTable.Frame.Y, PortsTable.Frame.Size.Width,
                                              (ViewModel.Shipment.VesselPorts.Count *  200) + 55);
                MainView.ContentSize = new CGSize(MainView.ContentSize.Width, 
                                                  MainView.ContentSize.Height + (ViewModel.Shipment.VesselPorts.Count *  200) + 55);
            }
        }
    }

我是 iOS 新手,我知道我的结构不好。 请提供建议,如何重新加载 TableViewScrollView。 请帮帮我。

【问题讨论】:

    标签: xcode uitableview xamarin uiscrollview xamarin.ios


    【解决方案1】:

    在 Xcode 中,旋转设备时使用此 API 更新您的 UI:

    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);
    

    使用 Objective-C 在 Xcode 中实现它,如下所示:

    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    
        [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    
        // Code here will execute before the rotation begins.
        // Equivalent to placing it in the deprecated method -[willRotateToInterfaceOrientation:duration:].
        [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
            // Place code here to perform animations during the rotation.
            // You can pass nil for this closure if not necessary.
            // Reorganize views, or move child view controllers.
            if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
    
                //Update UI
            }
    
            if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
    
                //Update UI
            }
    
        }completion:^(id<UIViewControllerTransitionCoordinatorContext> context){
                                         // Code here will execute after the rotation has finished.
                                         // Equivalent to placing it in the deprecated method -[didRotateFromInterfaceOrientation:].
                                         // Do any cleanup, if necessary.
    
                                     }];
    }
    

    它也可以在 Xamarin.iOS 中找到,ViewWillTransitionToSize。它可以像这样用 C# 实现:

        public override void ViewWillTransitionToSize(CGSize toSize, IUIViewControllerTransitionCoordinator coordinator)
        {
            base.ViewWillTransitionToSize(toSize, coordinator);
    
            coordinator.AnimateAlongsideTransition((IUIViewControllerTransitionCoordinatorContext) => {
    
                if (UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.Portrait || UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.PortraitUpsideDown)
                {
                    //Update UI
                }
    
                if (UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.LandscapeLeft|| UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.LandscapeRight)
                {
                    //Update UI
                }
    
            }, (IUIViewControllerTransitionCoordinatorContext) => {
                //Transition Completion
            });
        }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多