将超链接插入到UIAlertController';s消息文本(Xamarin.iOS)
UIAlertController使用UILabel显示文本,我读到只有UITextView可以显示可单击的链接。 我会在对话框中替换标签,但是一旦我删除它,程序就会抛出一个错误,它的约束无法激活。 要删除该约束,我在UILabel的SuperView中循环,但找不到它。 有人在这方面取得了成功吗将超链接插入到UIAlertController';s消息文本(Xamarin.iOS),xamarin.ios,dialog,uialertcontroller,Xamarin.ios,Dialog,Uialertcontroller,UIAlertController使用UILabel显示文本,我读到只有UITextView可以显示可单击的链接。 我会在对话框中替换标签,但是一旦我删除它,程序就会抛出一个错误,它的约束无法激活。 要删除该约束,我在UILabel的SuperView中循环,但找不到它。 有人在这方面取得了成功吗 UIAlertController dialog = UIAlertController.Create("", message, UIAlertControllerStyle.Alert); dial
UIAlertController dialog = UIAlertController.Create("", message, UIAlertControllerStyle.Alert);
dialog.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
UILabel label = (UILabel)dialog.View.Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[2];
UIView view = label;
do
{
view = view.Superview;
Console.WriteLine(view);
foreach (NSLayoutConstraint constraint in view.Constraints)
{
Console.WriteLine("Constraint: " + constraint.FirstItem + " --- " + constraint.FirstAttribute + " --- " + constraint.SecondItem + " --- " + constraint.SecondAttribute + " --- " + constraint.Constant);
if (constraint.FirstItem == label)
{
Console.WriteLine("FirstItem found");
view.RemoveConstraint(constraint);
}
if (constraint.SecondItem == label)
{
Console.WriteLine("SecondItem found");
view.RemoveConstraint(constraint);
}
}
} while (view != dialog.View);
//label.RemoveFromSuperview();
好的,您只需更改标签的可见性,然后插入textview。正如Ivan指出的,有一个备份解决方案是很好的
UIAlertController dialog = UIAlertController.Create("", message, UIAlertControllerStyle.Alert);
dialog.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
try
{
UILabel label = (UILabel)dialog.View.Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[2];
label.Hidden = true;
UITextView textView = new UITextView();
dialog.View.Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[0].InsertSubviewAbove(textView, label);
textView.TranslatesAutoresizingMaskIntoConstraints = false;
textView.LeadingAnchor.ConstraintEqualTo(label.LeadingAnchor).Active = true;
textView.TopAnchor.ConstraintEqualTo(label.TopAnchor, -8).Active = true;
textView.BackgroundColor = UIColor.FromRGBA(0, 0, 0, 0);
textView.ScrollEnabled = false;
textView.Editable = false;
textView.DataDetectorTypes = UIDataDetectorType.Link;
textView.Selectable = true;
textView.Font = UIFont.SystemFontOfSize(13);
textView.Text = message;
}
catch
{
}
context.PresentViewController(dialog, true, null);
(UILabel)对话框。视图。子视图[0]。子视图[0]。子视图[0]。子视图[0]。子视图[0]。子视图[0]。子视图[2];UIView=标签;-不要这样做。它可能会在任何版本的iOS中更改(即使是次要版本),并且您的应用程序可能会崩溃。在最后一种情况下,我将使用自定义对话框。它只是需要很多,使其外观和行为像内置的一个。