由于系统自带的AlertView和UIAlertController满足不了项目所需的要求,主要是这两个控件都实现不了,手动添加的三个Button按钮能横排显示在屏幕的中央,所以只能自己定义了!
1.首先新建一个UIView类,起名叫MyPopView
using System;
using System.Drawing;
using CoreGraphics;
using Foundation;
using UIKit;
namespace PoupUIView
{
[Register(“MyPopView”)]
public class MyPopView : UIView
{
public UIView view;
float m_width;
float m_height;
public MyPopView(RectangleF bounds) : base(bounds)
{
this.Frame = bounds;//铺满整个屏幕
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
//这种写法 不仅大View是透明的,小view也跟着透明了
//this.BackgroundColor = UIColor.Black;
//this.Alpha = 0.5f;
**this.BackgroundColor = UIColor.FromRGBA(0, 0, 0, 0.5f);** //让当前的View透明(遮罩层)
//动态获取当前屏幕的宽和高(以实现横竖屏幕时弹窗始终显示在屏幕的中央)
m_width = (float)UIScreen.MainScreen.Bounds.Width;
m_height = (float)UIScreen.MainScreen.Bounds.Height;
if (view == null)
{
view = new UIView(new CGRect((m_width - 337) / 2, (m_height - 206) / 2, 337, 206));
view.SizeToFit();
view.Layer.CornerRadius = 5;
view.BackgroundColor = UIColor.White;
view.Alpha = 1.0f;
this.AddSubview(view);
AddChildView();
}
}
private void AddChildView()
{
UILabel titleLabel = new UILabel(new CoreGraphics.CGRect(120, 18, 86, 31));
titleLabel.Text = "编辑批注";
titleLabel.TextColor = UIColor.Black;
titleLabel.SizeToFit();
view.AddSubview(titleLabel);
UILabel userInfo = new UILabel(new CoreGraphics.CGRect(45, 57, 242, 21));
userInfo.Text = "用户:李四时间:2018/3/7/15.00.18";
userInfo.Font = UIFont.SystemFontOfSize(13);
userInfo.TextColor = UIColor.Black;
userInfo.SizeToFit();
view.AddSubview(userInfo);
UIProgressView progressView = new UIProgressView(new CoreGraphics.CGRect(45, 98, 247, 9));
progressView.Progress = 100;
view.AddSubview(progressView);
UIButton button1 = new UIButton(new CoreGraphics.CGRect(45, 115, 67, 30));
button1.SetTitle("播放", UIControlState.Normal);
button1.TouchUpInside += Button1_TouchUpInside;
button1.BackgroundColor = UIColor.Gray;
UIButton button2 = new UIButton(new CoreGraphics.CGRect(132, 115, 67, 30));
button2.SetTitle("停止", UIControlState.Normal);
button2.TouchUpInside += Button2_TouchUpInside;
button2.BackgroundColor = UIColor.Gray;
UIButton button3 = new UIButton(new CoreGraphics.CGRect(221, 115, 67, 30));
button3.SetTitle("重录", UIControlState.Normal);
button3.TouchUpInside += Button3_TouchUpInside;
button3.BackgroundColor = UIColor.Gray;
view.AddSubview(button1);
view.AddSubview(button2);
view.AddSubview(button3);
UIButton button4 = new UIButton(new CoreGraphics.CGRect(20, 159, 62, 30));
button4.SetTitle("移动", UIControlState.Normal);
button4.SetTitleColor(UIColor.Black, UIControlState.Normal);
view.AddSubview(button4);
button4.TouchUpInside += Button4_TouchUpInside;
UIButton button5 = new UIButton(new CoreGraphics.CGRect(255, 159, 62, 30));
button5.SetTitle("删除", UIControlState.Normal);
button5.SetTitleColor(UIColor.Black, UIControlState.Normal);
view.AddSubview(button5);
button5.TouchUpInside += Button5_TouchUpInside;
}
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
//base.TouchesBegan(touches, evt);
UITouch touch = touches.AnyObject as UITouch;
if (touch.View == view)
{
return;
}
this.RemoveFromSuperview();
}
private void Button5_TouchUpInside(object sender, EventArgs e)
{
Console.WriteLine("您点击了删除!!!");
// this.Hidden = true; 让当前的大View隐藏
this.RemoveFromSuperview(); //移除当前的View
}
private void Button4_TouchUpInside(object sender, EventArgs e)
{
Console.WriteLine("您点击了移动!!!");
}
private void Button3_TouchUpInside(object sender, EventArgs e)
{
Console.WriteLine("您点击了重录!!!");
}
private void Button2_TouchUpInside(object sender, EventArgs e)
{
Console.WriteLine("您点击了停止!!!");
}
private void Button1_TouchUpInside(object sender, EventArgs e)
{
Console.WriteLine("您点击了播放!!!");
}
}
}
2.在ViewController中引用自己定义的UIView
using System;
using System.Drawing;
using UIKit;
namespace PoupUIView
{
public partial class MainViewController : UIViewController
{
private MyPopView m_mypopView;
public MainViewController() : base("MainViewController", null)
{
}
**public MyPopView MypopView
{
get
{
if (m_mypopView == null)
{
m_mypopView = new MyPopView(new RectangleF(0, 0, (float)UIScreen.MainScreen.Bounds.Width, (float)UIScreen.MainScreen.Bounds.Height));
}
return m_mypopView;
}
}**
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
//设置背景色
this.View.BackgroundColor = UIColor.Yellow;
popButton.TouchUpInside += PopButton_TouchUpInside;
}
private void PopButton_TouchUpInside(object sender, EventArgs e)
{
if (MypopView.Hidden)
{
MypopView.Hidden = false;
}
else
{
//this.View.Window.AddSubview(MypopView);
//在Button的点击事件中添加自定义view
this.View.AddSubview(MypopView);
}
}
}
}
3.程序运行效果截图如下:
4.设置当点击屏幕的空白区域时让弹窗消失
需要重写
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
//base.TouchesBegan(touches, evt);
UITouch touch = touches.AnyObject as UITouch;
//这里设置当触摸小view时,不让弹窗消失!
if (touch.View == view)
{
return;
}
// this.Hidden = true; 让当前的大View隐藏
this.RemoveFromSuperview();//否则就将让大View移除
}