目标:通过将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用于图像和swift
解决方案see。
答案 0 :(得分:2)
UIAlertViewController不能被子类化。
documentation的摘录中说:
通过使用UIViewController
在视图上具有透明性的子视图以及具有所需布局的子视图,您仍然可以获得所需的UI。
如果需要,您还需要分别设置以下两个属性:ModalTransitionStyle
和ModalPresentationStyle
分别为 UIModalTransitionStyle.CrossDissolve 和 UIModalPresentationStyle.OverCurrentContext 您的自定义UIAlertController的行为与UIAlertController
更新:
这就是我的意思:
在Main.Storyboard中放置一个 UIViewController 并根据需要更新设计。按照您上面发布的图像,我创建了如下所示的UI:
这是一张图像,两个
在UI上添加3按钮似乎是处理此问题的最简单方法,然后在将要显示警报时隐藏/显示它们。您还可以根据要显示的动作动态创建按钮。这取决于您。
创建一个ViewController类,我将其称为NiceAlertController,并将其分配给Storyboard中的ViewController。另外,请确保为所有UIControl(标签,按钮,图像等)创建后属性(插座),以便可以从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; }
为什么要使用这些属性?因为在您创建类时,视图上的控件尚不可用。仅在加载视图后,它们才可用。这就是为什么我们需要添加其他更改,例如下面的更改。
在这里,我们将值设置为用户界面上的控件
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中,您都可以像调用AlertViewController一样调用此ViewController:
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的想法。
希望这会有所帮助。-