MAUI iOS 自定义弹窗
MAUI iOS 自定义弹窗
MAUI iOS 自定义弹窗。
一、定义弹窗Class DialogCustomer
1 public partial class DialogCustomer 2 { 3 public partial Task<bool> CustomerAlertAsync(string title, string subTitle, string confirmMsg = "OK", string cancelMsg = "", string subTitle2 = "", string img = "", string textAlignment = "Center", int imgWidth = 0, int imgHeight = 0); 4 }
二、在平台下实现DialogCustomer Class
1 public partial class DialogCustomer 2 { 3 public partial Task<bool> CustomerAlertAsync(string title, string subTitle, string confirmMsg = "OK", string cancelMsg = "", string subTitle2 = "", string img = "", string textAlignment = "Center", int imgWidth = 0, int imgHeight = 0) 4 { 5 try 6 { 7 var tag = IndexTag.Instance.GetIndexTag(); 8 var tcs = new TaskCompletionSource<bool>(); 9 10 if (UIApplication.SharedApplication.GetTopWindow() != null) 11 { 12 var mainDisplayInfo = DeviceDisplay.MainDisplayInfo; 13 var density = mainDisplayInfo.Density; 14 var scanWidth = mainDisplayInfo.Width / density; 15 var scanHeight = mainDisplayInfo.Height / density; 16 17 if (Device.Idiom == TargetIdiom.Tablet) 18 { 19 scanWidth = scanWidth / 2; 20 } 21 22 float totalHeight = 0; 23 float contentHeight = 0; 24 float fixContentheight = 70; 25 float scrollHeight = 0; 26 float maxPopHeight = (float)scanHeight - 268; 27 bool visableScroll = false; 28 29 UILabel titleLbl = new UILabel(); 30 UIScrollView uIScroll = new UIScrollView(); 31 UILabel subTitleLbl2 = new UILabel(); 32 UIImageView imageView = new UIImageView(); 33 UILabel subTitleLbl = new UILabel(); 34 UIButton OKButton = new UIButton(); 35 UIButton CancleButton = new UIButton(); 36 37 UIView bgView = new UIView(new CGRect(0, 0, mainDisplayInfo.Width / density, scanHeight)); 38 bgView.BackgroundColor = UIColor.FromRGBA(0, 0, 0, 0.4f); 39 40 UIButton closeBtn = new UIButton(new CGRect(scanWidth - 70, 20, 22, 22)); 41 closeBtn.SetBackgroundImage(new UIImage("gw_alert_close.png"), UIControlState.Normal); 42 closeBtn.AddTarget((s, e) => 43 { 44 UIApplication.SharedApplication.GetTopWindow().ViewWithTag(tag).RemoveFromSuperview(); 45 tcs.SetResult(false); 46 }, UIControlEvent.TouchUpInside); 47 totalHeight = totalHeight + 62; 48 49 if (!string.IsNullOrEmpty(title)) 50 { 51 var titleSize = ItemSize(title, scanWidth - 70, 24, true); 52 titleLbl = new UILabel(new CGRect(22, totalHeight, scanWidth - 70, titleSize.Height)); 53 SetiOSUILabelFontFamily(titleLbl, 24f, true); 54 //titleLbl.LineBreakMode = UILineBreakMode.Clip; 55 titleLbl.Text = title; 56 titleLbl.TextAlignment = UITextAlignment.Center; 57 titleLbl.Lines = 0; 58 titleLbl.TextColor = UIColor.Black; 59 totalHeight = totalHeight + (float)titleSize.Height + 32; 60 fixContentheight = fixContentheight + (float)titleSize.Height + 32; 61 } 62 63 if (!string.IsNullOrEmpty(subTitle2)) 64 { 65 var subTitleSize = ItemSize(subTitle2, scanWidth - 70, 16); 66 contentHeight = contentHeight + (float)subTitleSize.Height + 4; 67 } 68 if (!string.IsNullOrEmpty(img)) 69 { 70 var deviceImg = new UIImage(img); 71 if (imgWidth > 0 && imgHeight > 0) 72 contentHeight = contentHeight + (float)imgHeight + 4; 73 else 74 contentHeight = contentHeight + (float)deviceImg.Size.Height + 4; 75 } 76 if (!string.IsNullOrEmpty(subTitle)) 77 { 78 var subTitleSize = ItemSize(subTitle, scanWidth - 70, 16); 79 contentHeight = contentHeight + (float)subTitleSize.Height + 34; 80 } 81 82 if ((fixContentheight + contentHeight + 55 + 24) > maxPopHeight) 83 { 84 visableScroll = true; 85 scrollHeight = maxPopHeight - (fixContentheight + 55 + 24); 86 } 87 88 if (visableScroll) 89 { 90 uIScroll = new UIScrollView(new CGRect(0, totalHeight, scanWidth - 32, scrollHeight)); 91 uIScroll.ContentSize = new CGSize(scanWidth - 70, contentHeight); 92 totalHeight = totalHeight + scrollHeight + 24; 93 float contentTotalHeight = 0; 94 95 if (!string.IsNullOrEmpty(subTitle2)) 96 { 97 var subTitleSize = ItemSize(subTitle2, scanWidth - 70, 16); 98 subTitleLbl2 = new UILabel(new CGRect(22, contentTotalHeight, scanWidth - 70, subTitleSize.Height)); 99 SetiOSUILabelFontFamily(subTitleLbl2, 16, false); 100 subTitleLbl2.Text = subTitle2; 101 subTitleLbl2.LineBreakMode = UILineBreakMode.Clip; 102 subTitleLbl2.Lines = 0; 103 switch (textAlignment) 104 { 105 case "Left": 106 subTitleLbl2.TextAlignment = UITextAlignment.Left; 107 break; 108 case "Right": 109 subTitleLbl2.TextAlignment = UITextAlignment.Right; 110 break; 111 default: 112 subTitleLbl2.TextAlignment = UITextAlignment.Center; 113 break; 114 } 115 subTitleLbl2.TextColor = UIColor.Black; 116 contentTotalHeight = contentTotalHeight + (float)subTitleSize.Height + 4; 117 uIScroll.AddSubview(subTitleLbl2); 118 } 119 120 if (!string.IsNullOrEmpty(img)) 121 { 122 if (imgWidth > 0 && imgHeight > 0) 123 { 124 var deviceImg = new UIImage(img); 125 var imgx = (scanWidth - 32 - imgWidth) / 2; 126 imageView = new UIImageView(deviceImg); 127 imageView.Frame = new CGRect(imgx, contentTotalHeight, imgWidth, imgHeight); 128 contentTotalHeight = contentTotalHeight + (float)imgHeight + 4; 129 uIScroll.AddSubview(imageView); 130 } 131 else 132 { 133 var deviceImg = new UIImage(img); 134 var imgx = (scanWidth - 32 - deviceImg.Size.Width) / 2; 135 imageView = new UIImageView(deviceImg); 136 imageView.Frame = new CGRect(imgx, contentTotalHeight, deviceImg.Size.Width, deviceImg.Size.Height); 137 contentTotalHeight = contentTotalHeight + (float)deviceImg.Size.Height + 4; 138 uIScroll.AddSubview(imageView); 139 } 140 141 } 142 143 if (!string.IsNullOrEmpty(subTitle)) 144 { 145 var subTitleSize = ItemSize(subTitle, scanWidth - 70, 16); 146 subTitleLbl = new UILabel(new CGRect(22, contentTotalHeight, scanWidth - 70, subTitleSize.Height)); 147 SetiOSUILabelFontFamily(subTitleLbl, 16, false); 148 subTitleLbl.Text = subTitle; 149 subTitleLbl.LineBreakMode = UILineBreakMode.Clip; 150 subTitleLbl.Lines = 0; 151 switch (textAlignment) 152 { 153 case "Left": 154 subTitleLbl.TextAlignment = UITextAlignment.Left; 155 break; 156 case "Right": 157 subTitleLbl.TextAlignment = UITextAlignment.Right; 158 break; 159 default: 160 subTitleLbl.TextAlignment = UITextAlignment.Center; 161 break; 162 } 163 subTitleLbl.TextColor = UIColor.Black; 164 contentTotalHeight = contentTotalHeight + (float)subTitleSize.Height + 34; 165 uIScroll.AddSubview(subTitleLbl); 166 } 167 168 if (string.IsNullOrEmpty(cancelMsg)) 169 { 170 OKButton = new UIButton(new CGRect(16, totalHeight, scanWidth - 64, 55)); 171 SetiOSUIButtonFontFamily(OKButton, 16, false); 172 OKButton.Layer.CornerRadius = 5; 173 OKButton.Layer.MasksToBounds = true; 174 OKButton.SetTitle(confirmMsg == "OK" ? AppLanguage.OK : confirmMsg, UIControlState.Normal); 175 OKButton.SetTitleColor(UIColor.White, UIControlState.Normal); 176 OKButton.BackgroundColor = UIColor.Black; 177 OKButton.AddTarget((s, e) => 178 { 179 UIApplication.SharedApplication.GetTopWindow().ViewWithTag(tag).RemoveFromSuperview(); 180 tcs.SetResult(true); 181 }, UIControlEvent.TouchUpInside); 182 183 totalHeight = totalHeight + 55 + 24; 184 fixContentheight = fixContentheight + 55 + 24; 185 } 186 else 187 { 188 var buttonWidth = (scanWidth - 80) / 2; 189 CancleButton = new UIButton(new CGRect(18, totalHeight, buttonWidth, 55)); 190 SetiOSUIButtonFontFamily(CancleButton, 16, false); 191 CancleButton.Layer.CornerRadius = 5; 192 CancleButton.Layer.MasksToBounds = true; 193 CancleButton.Layer.BorderWidth = 2; 194 CancleButton.Layer.BorderColor = new CGColor(0, 0, 0); 195 CancleButton.SetTitle(cancelMsg, UIControlState.Normal); 196 CancleButton.SetTitleColor(UIColor.Black, UIControlState.Normal); 197 CancleButton.BackgroundColor = UIColor.FromRGB(255, 255, 255); 198 CancleButton.AddTarget((s, e) => 199 { 200 UIApplication.SharedApplication.GetTopWindow().ViewWithTag(tag).RemoveFromSuperview(); 201 tcs.SetResult(false); 202 }, UIControlEvent.TouchUpInside); 203 204 OKButton = new UIButton(new CGRect(28 + buttonWidth, totalHeight, buttonWidth, 55)); 205 SetiOSUIButtonFontFamily(OKButton, 16, false); 206 OKButton.Layer.CornerRadius = 5; 207 OKButton.Layer.MasksToBounds = true; 208 OKButton.SetTitle(confirmMsg == "OK" ? AppLanguage.OK : confirmMsg, UIControlState.Normal); 209 OKButton.SetTitleColor(UIColor.White, UIControlState.Normal); 210 OKButton.BackgroundColor = UIColor.Black; 211 OKButton.AddTarget((s, e) => 212 { 213 UIApplication.SharedApplication.GetTopWindow().ViewWithTag(tag).RemoveFromSuperview(); 214 tcs.SetResult(true); 215 }, UIControlEvent.TouchUpInside); 216 217 totalHeight = totalHeight + 55 + 24; 218 fixContentheight = fixContentheight + 55 + 24; 219 } 220 221 UIView contentView = new UIView(new CGRect(0, 60, scanWidth - 32, totalHeight)); 222 contentView.BackgroundColor = UIColor.White; 223 contentView.Alpha = 1f; 224 contentView.Layer.CornerRadius = 5; 225 contentView.Layer.MasksToBounds = true; 226 contentView.Center = UIApplication.SharedApplication.GetTopWindow().Center; 227 228 229 contentView.AddSubview(closeBtn); 230 if (!string.IsNullOrEmpty(title)) 231 contentView.AddSubview(titleLbl); 232 233 234 contentView.AddSubview(uIScroll); 235 236 if (string.IsNullOrEmpty(cancelMsg)) 237 contentView.AddSubview(OKButton); 238 else 239 { 240 contentView.AddSubview(OKButton); 241 contentView.AddSubview(CancleButton); 242 } 243 bgView.AddSubview(contentView); 244 bgView.Tag = tag; 245 246 UIApplication.SharedApplication.GetTopWindow().AddSubview(bgView); 247 } 248 else 249 { 250 if (!string.IsNullOrEmpty(subTitle2)) 251 { 252 var subTitleSize = ItemSize(subTitle2, scanWidth - 70, 16); 253 subTitleLbl2 = new UILabel(new CGRect(22, totalHeight, scanWidth - 70, subTitleSize.Height)); 254 SetiOSUILabelFontFamily(subTitleLbl2, 16, false); 255 subTitleLbl2.Text = subTitle2; 256 subTitleLbl2.LineBreakMode = UILineBreakMode.Clip; 257 subTitleLbl2.Lines = 0; 258 switch (textAlignment) 259 { 260 case "Left": 261 subTitleLbl2.TextAlignment = UITextAlignment.Left; 262 break; 263 case "Right": 264 subTitleLbl2.TextAlignment = UITextAlignment.Right; 265 break; 266 default: 267 subTitleLbl2.TextAlignment = UITextAlignment.Center; 268 break; 269 } 270 subTitleLbl2.TextColor = UIColor.Black; 271 totalHeight = totalHeight + (float)subTitleSize.Height + 4; 272 273 } 274 275 if (!string.IsNullOrEmpty(img)) 276 { 277 if (imgWidth > 0 && imgHeight > 0) 278 { 279 var deviceImg = new UIImage(img); 280 var imgx = (scanWidth - 32 - imgWidth) / 2; 281 imageView = new UIImageView(deviceImg); 282 imageView.Frame = new CGRect(imgx, totalHeight, imgWidth, imgHeight); 283 totalHeight = totalHeight + (float)imgHeight + 24; 284 } 285 else 286 { 287 var deviceImg = new UIImage(img); 288 var imgx = (scanWidth - 32 - deviceImg.Size.Width) / 2; 289 imageView = new UIImageView(deviceImg); 290 imageView.Frame = new CGRect(imgx, totalHeight, deviceImg.Size.Width, deviceImg.Size.Height); 291 totalHeight = totalHeight + (float)deviceImg.Size.Height + 24; 292 } 293 } 294 295 if (!string.IsNullOrEmpty(subTitle)) 296 { 297 var subTitleSize = ItemSize(subTitle, scanWidth - 70, 16); 298 subTitleLbl = new UILabel(new CGRect(22, totalHeight, scanWidth - 70, subTitleSize.Height)); 299 SetiOSUILabelFontFamily(subTitleLbl, 16, false); 300 subTitleLbl.Text = subTitle; 301 subTitleLbl.LineBreakMode = UILineBreakMode.Clip; 302 subTitleLbl.Lines = 0; 303 switch (textAlignment) 304 { 305 case "Left": 306 subTitleLbl.TextAlignment = UITextAlignment.Left; 307 break; 308 case "Right": 309 subTitleLbl.TextAlignment = UITextAlignment.Right; 310 break; 311 default: 312 subTitleLbl.TextAlignment = UITextAlignment.Center; 313 break; 314 } 315 subTitleLbl.TextColor = UIColor.Black; 316 totalHeight = totalHeight + (float)subTitleSize.Height + 24; 317 318 } 319 320 if (string.IsNullOrEmpty(cancelMsg)) 321 { 322 OKButton = new UIButton(new CGRect(16, totalHeight, scanWidth - 64, 55)); 323 SetiOSUIButtonFontFamily(OKButton, 16, false); 324 OKButton.Layer.CornerRadius = 5; 325 OKButton.Layer.MasksToBounds = true; 326 OKButton.SetTitle(confirmMsg == "OK" ? AppLanguage.OK : confirmMsg, UIControlState.Normal); 327 OKButton.SetTitleColor(UIColor.White, UIControlState.Normal); 328 OKButton.BackgroundColor = UIColor.Black; 329 OKButton.AddTarget((s, e) => 330 { 331 UIApplication.SharedApplication.GetTopWindow().ViewWithTag(tag).RemoveFromSuperview(); 332 tcs.SetResult(true); 333 }, UIControlEvent.TouchUpInside); 334 335 totalHeight = totalHeight + 55 + 24; 336 fixContentheight = fixContentheight + 55 + 24; 337 } 338 else 339 { 340 var buttonWidth = (scanWidth - 80) / 2; 341 CancleButton = new UIButton(new CGRect(18, totalHeight, buttonWidth, 55)); 342 SetiOSUIButtonFontFamily(CancleButton, 16, false); 343 CancleButton.Layer.CornerRadius = 5; 344 CancleButton.Layer.MasksToBounds = true; 345 CancleButton.Layer.BorderWidth = 2; 346 CancleButton.Layer.BorderColor = new CGColor(0, 0, 0); 347 CancleButton.SetTitle(cancelMsg, UIControlState.Normal); 348 CancleButton.SetTitleColor(UIColor.Black, UIControlState.Normal); 349 CancleButton.BackgroundColor = UIColor.FromRGB(255, 255, 255); 350 CancleButton.AddTarget((s, e) => 351 { 352 UIApplication.SharedApplication.GetTopWindow().ViewWithTag(tag).RemoveFromSuperview(); 353 tcs.SetResult(false); 354 }, UIControlEvent.TouchUpInside); 355 356 OKButton = new UIButton(new CGRect(28 + buttonWidth, totalHeight, buttonWidth, 55)); 357 SetiOSUIButtonFontFamily(OKButton, 16, false); 358 OKButton.Layer.CornerRadius = 5; 359 OKButton.Layer.MasksToBounds = true; 360 OKButton.SetTitle(confirmMsg == "OK" ? AppLanguage.OK : confirmMsg, UIControlState.Normal); 361 OKButton.SetTitleColor(UIColor.White, UIControlState.Normal); 362 OKButton.BackgroundColor = UIColor.Black; 363 OKButton.AddTarget((s, e) => 364 { 365 UIApplication.SharedApplication.GetTopWindow().ViewWithTag(tag).RemoveFromSuperview(); 366 tcs.SetResult(true); 367 }, UIControlEvent.TouchUpInside); 368 369 totalHeight = totalHeight + 55 + 24; 370 fixContentheight = fixContentheight + 55 + 24; 371 } 372 373 UIView contentView = new UIView(new CGRect(0, 60, scanWidth - 32, totalHeight)); 374 contentView.BackgroundColor = UIColor.White; 375 contentView.Alpha = 1f; 376 contentView.Layer.CornerRadius = 5; 377 contentView.Layer.MasksToBounds = true; 378 contentView.Center = UIApplication.SharedApplication.GetTopWindow().Center; 379 380 381 contentView.AddSubview(closeBtn); 382 if (!string.IsNullOrEmpty(title)) 383 contentView.AddSubview(titleLbl); 384 if (!string.IsNullOrEmpty(subTitle2)) 385 contentView.AddSubview(subTitleLbl2); 386 if (!string.IsNullOrEmpty(img)) 387 contentView.AddSubview(imageView); 388 if (!string.IsNullOrEmpty(subTitle)) 389 contentView.AddSubview(subTitleLbl); 390 if (string.IsNullOrEmpty(cancelMsg)) 391 contentView.AddSubview(OKButton); 392 else 393 { 394 contentView.AddSubview(OKButton); 395 contentView.AddSubview(CancleButton); 396 } 397 bgView.AddSubview(contentView); 398 bgView.Tag = tag; 399 //UIView.Animate(1f, () => 400 //{ 401 //UIApplication.SharedApplication.GetTopWindow().Alpha = 1.0f; 402 //UIApplication.SharedApplication.GetTopWindow() 403 //bgView.Alpha = 1.0f; 404 //bgView?.CenterYAnchor = bgView.HeightAnchor - (bgView.he) 405 //}); 406 407 UIApplication.SharedApplication.GetTopWindow().AddSubview(bgView); 408 } 409 } 410 else 411 { 412 tcs.SetResult(false); 413 } 414 return tcs.Task; 415 416 } 417 catch (Exception ex) 418 { 419 return null; 420 } 421 } 422 423 private CGSize ItemSize(string title, double width, float font, bool boldFont = false) 424 { 425 var lbl = new UILabel(new CGRect(0, 0, width, 0)); 426 lbl.Lines = 0; 427 //lbl.LineBreakMode = UILineBreakMode.WordWrap; 428 SetiOSUILabelFontFamily(lbl, font, boldFont); 429 lbl.Text = title; 430 //lbl.TextAlignment = UITextAlignment.Center; 431 lbl.SizeToFit(); 432 return new CGSize(width, lbl.Frame.Size.Height + 10); 433 } 434 435 436 437 private void SetiOSUILabelFontFamily(UILabel uILabel, NFloat fontSize, bool isBlod = false, AlertElementFontStyle alertElementFontStyle = AlertElementFontStyle.Normol) 438 { 439 if (isBlod) 440 { 441 //alertElementFontStyle = AlertElementFontStyle.Bold; 442 uILabel.Font = UIFont.FromName(@"Helvetica-Bold", fontSize); 443 } 444 else 445 { 446 //uILabel.Font = UIFont.FromName("SFProDisplay-Regular", fontSize); 447 switch (alertElementFontStyle) 448 { 449 case AlertElementFontStyle.Normol: 450 uILabel.Font = UIFont.SystemFontOfSize(fontSize); 451 break; 452 case AlertElementFontStyle.Bold: 453 uILabel.Font = UIFont.BoldSystemFontOfSize(fontSize); 454 break; 455 case AlertElementFontStyle.Italic: 456 uILabel.Font = UIFont.ItalicSystemFontOfSize(fontSize); 457 break; 458 default: 459 break; 460 } 461 } 462 463 } 464 465 466 private void SetiOSUIButtonFontFamily(UIButton uIButton, NFloat fontSize, bool isBlod = false) 467 { 468 //if (isBlod) 469 //{ 470 // uIButton.Font = UIFont.FromName(@"SFProDisplay-Bold", fontSize); 471 //} 472 //else 473 //{ 474 // uIButton.Font = UIFont.FromName("SFProDisplay-Regular", fontSize); 475 //} 476 } 477 478 479 } 480 481 public class IndexTag 482 { 483 public int Index = 0; 484 private static readonly object objlocktag = new object(); 485 private static IndexTag instance = null; 486 private IndexTag() 487 { 488 Index = int.Parse(DateTime.Now.ToString("yyyyMMddHH")); 489 } 490 public static IndexTag Instance 491 { 492 get 493 { 494 lock (objlocktag) 495 { 496 if (instance == null) 497 { 498 instance = new IndexTag(); 499 } 500 } 501 return instance; 502 } 503 } 504 505 public int GetIndexTag() 506 { 507 return Index++; 508 } 509 } 510 511 512 public static class Utils 513 { 514 public static UIWindow GetTopWindow(this UIApplication app) => app 515 .Windows 516 .Reverse() 517 .FirstOrDefault(x => 518 x.WindowLevel == UIWindowLevel.Normal && 519 !x.Hidden 520 ); 521 522 523 public static UIView GetTopView(this UIApplication app) => app.GetTopWindow().Subviews.Last(); 524 525 526 public static UIViewController GetTopViewController(this UIApplication app) 527 { 528 var viewController = app.KeyWindow.RootViewController; 529 while (viewController.PresentedViewController != null) 530 viewController = viewController.PresentedViewController; 531 532 return viewController; 533 } 534 }
三、调用DialogCustomer Class
1 MainThread.BeginInvokeOnMainThread(async () => 2 { 3 var res = await new DialogCustomer().CustomerAlertAsync("title", "在 .NET 多平台应用 UI (.NET MAUI) 不提供任何用于访问特定平台 API 的 API 的情况下,可以编写自己的代码来访问所需的平台 API。 这需要了解 Apple 的 iOS 和 MacCatalyst API、 Google 的 Android API 和 Microsoft 的 Windows 应用 SDK API。在 .NET 多平台应用 UI (.NET MAUI) 不提供任何用于访问特定平台 API 的 API 的情况下,可以编写自己的代码来访问所需的平台 API。 这需要了解 Apple 的 iOS 和 MacCatalyst API、 Google 的 Android API 和 Microsoft 的 Windows 应用 SDK API。在 .NET 多平台应用 UI (.NET MAUI) 不提供任何用于访问特定平台 API 的 API 的情况下,可以编写自己的代码来访问所需的平台 API。 这需要了解 Apple 的 iOS 和 MacCatalyst API、 Google 的 Android API 和 Microsoft 的 Windows 应用 SDK API。", confirmMsg: "OK", cancelMsg: "cancel", img: "accept.png"); 4 });
GitHub URL:https://github.com/zuimengaitianya/GLBMaui 。喜欢的话点个star。
欢迎分享MAUI文章。