目标:通过继承UIAlertController's
并在标题字符串中添加新的行字符UIAlertController
,在\n
标题标签上方添加图像,为UIImageView
腾出空间
可以看出,能够成功地将图像添加到UIAlertController
但图像没有间距/位于标题上方。它似乎是添加到警报的中心。如何在UIAlertController标题上正确分隔图像?
当前代码:
namespace XamarinFormsApp1.Extensions
{
public class AlertController : UIAlertController
{
private string originalTitle;
private string spaceAdjustedTitle;
private UIImageView imageView = null;
private CoreGraphics.CGSize previousImgViewSize
= CoreGraphics.CGSize.Empty;
public override UIAlertControllerStyle PreferredStyle
{
get
{
return UIAlertControllerStyle.Alert;
}
}
public override string Title
{
get
{
return originalTitle;
}
set
{
if (Title != spaceAdjustedTitle ||
string.IsNullOrEmpty(Title) ||
string.IsNullOrEmpty(spaceAdjustedTitle))
{
originalTitle = value;
}
}
}
public void setTitleImage(UIImage image)
{
if (this.imageView == null)
{
UIImageView imageView = new UIImageView(image);
this.View.AddSubview(imageView);
this.imageView = imageView;
return;
}
imageView.Image = image;
}
public override void ViewDidLayoutSubviews()
{
if (imageView == null)
{
base.ViewDidLayoutSubviews();
return;
}
// Adjust title if image size has changed
if (previousImgViewSize != imageView.Bounds.Size)
{
previousImgViewSize = imageView.Bounds.Size;
adjustTitle(imageView);
}
// Position `imageView`
var linesCount = newLinesCount(imageView);
var padding = Constants.Padding(PreferredStyle);
var x = View.Bounds.Width / 2.0;
var y = padding + linesCount * lineHeight / 2.0;
CoreGraphics.CGPoint cgPoint = imageView.Center;
cgPoint.X = (nfloat)x;
cgPoint.Y = (nfloat)y;
imageView.Center = cgPoint;
base.ViewDidLayoutSubviews();
}
private void adjustTitle(UIImageView imageView)
{
var linesCount = (int)newLinesCount(imageView);
var lines = Enumerable
.Range(1, linesCount)
.Select(i => "\n")
.Aggregate((c, n) => $"{c}{n}");
spaceAdjustedTitle = lines + (originalTitle ?? "");
Title = spaceAdjustedTitle;
}
private double newLinesCount(UIImageView imageView)
{
return Math.Ceiling(
imageView.Bounds.Height / lineHeight);
}
private float lineHeight
{
get
{
UIFontTextStyle style = this.PreferredStyle
== UIAlertControllerStyle.Alert
? UIFontTextStyle.Headline
: UIFontTextStyle.Callout;
return (float)UIFont
.GetPreferredFontForTextStyle(style)
.PointSize;
}
}
struct Constants
{
static float paddingAlert = 22;
static float paddingSheet = 11;
public static float Padding(UIAlertControllerStyle style)
{
return style == UIAlertControllerStyle.Alert
? Constants.paddingAlert
: Constants.paddingSheet;
}
}
}
}
注意:感谢@stringCode for image和swift
解决方案,see。
UIAlertViewController并不意味着是子类。
来自documentation的摘录说:
您仍然可以通过使用视图上具有透明度的UIViewController
和具有所需布局的子视图来获得所需的UI。
如果您希望自定义UIAlertController的行为与ModalTransitionStyle
相同,您还需要将这两个属性:ModalPresentationStyle
和UIAlertController
分别设置为UIModalTransitionStyle.CrossDissolve和UIModalPresentationStyle.OverCurrentContext。
更新:
这就是我的意思,你可以这样做:
在Main.Storyboard中,删除UIViewController并根据需要更新设计。按照您在上面发布的图片我创建了UI,如下所示:
这是一个图像,标题和消息的2个UILabel和3个不同动作的3个按钮(默认,销毁,取消)。所有这些控件都在带有白色背景的UIView中。对于示例,我将其称为ContentView
在UI上添加3按钮似乎是使用此功能的最简单方法,然后在您即将显示警报时隐藏/显示它们。您还可以根据要显示的操作动态创建按钮。这取决于你。
创建一个ViewController类,我称之为NiceAlertController,并将其分配给Storyboard中的ViewController。此外,请确保为所有UIControl(标签,按钮,图像等)创建后退属性(Outlets),以便您可以从ViewController类访问它。
Here有关如何在设计器上使用iOS Storyboard的更多信息
现在,在课堂上,您需要添加代码才能使其正常工作。
在您的类中,要使视图透明,您需要将其添加到ViewDidLoad方法:
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.View.BackgroundColor = UIColor.Clear.ColorWithAlpha(0.2f);
this.View.Opaque = false;
}
此外,我们可以模仿UIAlertControllers的创建方式,并创建我们的方法:
public static NiceAlertController Create(string title, string message, UIImage image = null)
{
//New instance of your ViewController UI
var storyboard = UIStoryboard.FromName("Main", NSBundle.MainBundle);
var alertController = storyboard.InstantiateViewController(nameof(NiceAlertController)) as NiceAlertController;
//Using the same transition and presentation style as the UIAlertViewController
alertController.ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
alertController.ModalPresentationStyle = UIModalPresentationStyle.OverCurrentContext;
//This assumes your properties are called Title, Message and Image
alertController.Title = title;
alertController.Message = message;
alertController.Image = image;
return alertController;
}
上面使用的3个属性(标题,消息和图像)如下所示:
public new string Title { get; private set; }
public string Message { get; private set; }
public UIImage Image { get; private set; }
为什么这些属性?因为在您创建类时,视图上的控件尚不可用。只有在加载View后,它们才可用。这就是为什么我们需要添加其他更改,如下所示。
这里我们将值设置为UI上的Controls
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.titleLabel.Text = Title;
this.messageLabel.Text = Message;
//If you don't set an image while Create, it will use the default image you set on the Designer.
if (Image != null)
{
this.imageView.Image = Image;
}
}
现在,您可以从任何其他ViewController调用此ViewController,就像调用AlertViewController一样:
private void ShowMyAlertController()
{
var alert = NiceAlertController.Create("Hello", "My nice alert controller");
this.PresentViewController(alert, true, null);
}
它看起来应该是这样的:
要处理操作(点击按钮时要执行的操作),您可以创建以下特定方法:
public void AddDefaultAction(string title, Action action)
{
//Logic here
}
public void AddCancelAction(string title, Action action)
{
//Logic here
}
public void AddDestructiveAction(string title, Action action)
{
//Logic here
}
希望这能让您了解如何创建自定义UIViewcontroller并使其看起来像UIAlertViewController。
希望这可以帮助。-