Xamarin.iOS 下拉刷新样式的修改

MvvmCross框架 提供了下拉刷新的控件:RefreshTableHeaderView,也就是一个View,但是前几天做项目的时候发现她的样式没办法修改。它内部控件展示的都是一些英文,而且没有左侧下拉箭头的样式。

最后翻看他的源码,发现这个类中一些主要的方法可以复写,于是我决定复写他的方法修改样式。


首先ViewController要继承自DialogViewController,复写MakeRefreshTableHeaderView方法:


	/// <summary>
        /// 重写创建刷新view的方法
        /// </summary>
        /// <returns>The refresh table header view.</returns>
        /// <param name="rect">Rect.</param>
        public override RefreshTableHeaderView MakeRefreshTableHeaderView(CoreGraphics.CGRect rect)
        {
            var headView = new BabyRefreshHeadView(rect);

            return headView;
        }


其中BabyRefreshHeadView是继承自系统提供的RefreshTableHeaderView,代码在下面,下一步是在ViewController中指定刷新事件:


            //刷新方法
            RefreshRequested += delegate {
                ViewModel.LoadLatestDataList();
            };


在构造方法或者页面加载方法中都可以。

最后贴上继承自RefreshTableHeaderView的类,在其中自定义我们自己想要的语言文字和图片等。来达到修改下拉刷新样式的目的。


using System;
using CoreAnimation;
using CoreGraphics;
using CrossUI.iOS.Dialog.Utilities;
using UIKit;

namespace RuilaiGrow.iOS
{
    /// <summary>
    /// 本项目的下啦刷新的view
    /// by ge
    /// </summary>
    public class BabyRefreshHeadView : RefreshTableHeaderView
    {
        public BabyRefreshHeadView(CGRect rect) : base(rect)
        {
            //设置背景颜色
            BackgroundColor = MvxTouchColor.ShallowGray;
        }

        //刷新下啦箭头
        private static readonly UIImage arrow = UIImage.FromBundle("arrow" + ".png");

        /// <summary>
        /// 重写父类创建view的方法 并屏蔽父类view的创建
        /// </summary>
        public override void CreateViews()
        {
            //base.CreateViews();
    
            //上次刷新时间文字
            LastUpdateLabel = new UILabel
            {
                Font = UIFont.SystemFontOfSize(13f),
                TextColor = new UIColor(0.47f, 0.50f, 0.57f, 1),
                ShadowColor = UIColor.White,
                ShadowOffset = new CGSize(0, 1),
                BackgroundColor = MvxTouchColor.ShallowGray,
                Opaque = true,
                TextAlignment = UITextAlignment.Center,
                AutoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin
            };
            AddSubview(LastUpdateLabel);

            //刷新状态文字
            StatusLabel = new UILabel
            {
                Font = UIFont.BoldSystemFontOfSize(14),
                TextColor = new UIColor(0.47f, 0.50f, 0.57f, 1),
                ShadowColor = LastUpdateLabel.ShadowColor,
                ShadowOffset = new CGSize(0, 1),
                BackgroundColor = MvxTouchColor.ShallowGray,
                Opaque = true,
                TextAlignment = UITextAlignment.Center,
                AutoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin
            };
            AddSubview(StatusLabel);
            SetStatus(RefreshViewStatus.PullToReload);

            //右侧的image
            ArrowView = new UIImageView
            {
                ContentMode = UIViewContentMode.ScaleAspectFill,
                Image = arrow,
                AutoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin
            };
            ArrowView.Layer.Transform = CATransform3D.MakeRotation((float)Math.PI, 0, 0, 1);
            AddSubview(ArrowView);

            Activity = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.Gray)
            {
                HidesWhenStopped = true,
                AutoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin
            };
            AddSubview(Activity);
            
        }

        private RefreshViewStatus status = (RefreshViewStatus)(-1);

        /// <summary>
        /// 重写父类状态的方法 并屏蔽父类的代码
        /// 目的设置中文我们自己想要的文字
        /// </summary>
        /// <param name="status">Status.</param>
        public override void SetStatus(RefreshViewStatus status)
        {
            //base.SetStatus(status);
            if (this.status == status)
                return;

            string s = "松开刷新";

            switch (status)
            {
                case RefreshViewStatus.Loading:
                    s = "加载中...";
                    break;

                case RefreshViewStatus.PullToReload:
                    s = "下拉刷新...";
                    break;
            }
            StatusLabel.Text = s;
        }

        /// <summary>
        /// 设置上次刷新时间的文字
        /// 暂时没有用到
        /// </summary>
        private DateTime lastUpdateTime;

        public DateTime LastUpdate
        {
            get { return lastUpdateTime; }
            set
            {
                if (value == lastUpdateTime)
                    return;

                lastUpdateTime = value;
                if (value == DateTime.MinValue)
                {
                    LastUpdateLabel.Text = "上次更新: 没有";
                }
                else
                    LastUpdateLabel.Text = String.Format("上次更新: {0:g}", value);
            }
        }
    }
}







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值