您的位置:首页 > Web前端 > CSS

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

2016-12-08 17:22 225 查看
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);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息