Monotouch几个常用控件的实例代码
一、导航条 UINavigationController
- 简介:主要用于页面的导航,可以用于跳转页面及页面使用导航条上的按钮。初次运行程序 加载UINavigationController
1 public override bool FinishedLaunching(UIApplication app, NSDictionary options) { 2 window = new UIWindow(UIScreen.MainScreen.Bounds); 3 //NavMainViewController继承自UINavigationController 4 viewController = new NavMainViewController(); 5 //程序开始运行时会将NavMainViewController作为主界面。 6 //而NavMainViewController通过PushViewController方法显示要呈现的uiviewcontroller页面 7 window.RootViewController = viewController; 8 window.MakeKeyAndVisible(); 9 return true; 10 }
- 跳转页面的代码:
1 public class NavMainViewController:UINavigationController 2 { 3 public override void ViewDidLoad () 4 { 5 base.ViewDidLoad (); 6 //MusicPlayView是显示程序的主界面 继承自UIViewController 7 this.PushViewController(new MusicPlayView(),true); 8 //默认导航条的导航栏不显示 9 this.NavigationBarHidden=true; 10 } 11 }
- 使用导航条顶部按钮的代码:
1 //创建UIBarbuttonItem 2 UIBarButtonItem barEvaluate=new UIBarButtonItem(); 3 barEvaluate.Title="评价我们"; 4 barEvaluate.Style=UIBarButtonItemStyle.Bordered; 5 barEvaluate.Clicked+=(sender,e)=>{ 6 //打开应用商店 7 var url="http://itunes.apple.com/"; 8 UIApplication.SharedApplication.OpenUrl(NSUrl.FromString(url)); 9 }; 10 //设置头部导航条的右侧按钮 11 this.NavigationItem.RightBarButtonItem=barEvaluate;
- 使用导航条底部按钮的代码:
1 //使用UIBarButtonSystemItem实例化 点击底部工具栏btnPrevious时会有高亮效果 2 UIBarButtonItem btnPrevious=new UIBarButtonItem(UIBarButtonSystemItem.Rewind); 3 btnPrevious.Clicked+=(sender,e)=>{ 4 5 }; 6 //用于调整按钮间的间距控件 7 UIBarButtonItem btnSpace=new UIBarButtonItem(UIBarButtonSystemItem.FixedSpace); 8 //每个Item的默认Size为30*30 9 btnSpace.Width=this.View.Frame.Width/2-30f; 10 UIBarButtonItem btnNext=new UIBarButtonItem(UIBarButtonSystemItem.FastForward); 11 btnNext.Clicked+=delegate{ 12 13 }; 14 this.ToolbarItems=new UIBarButtonItem[]{btnSpace,btnPrevious,btnSpace,btnNext}; 15 //显示底部工具栏 16 this.NavigationController.ToolbarHidden=false; 17 //调整顶部和底部工具栏的显示样式 18 this.NavigationController.NavigationBar.BarStyle=UIBarStyle.Black; 19 this.NavigationController.NavigationBar.Opaque=true; 20 this.NavigationController.Toolbar.BarStyle=UIBarStyle.Black; 21 this.NavigationController.Toolbar.Opaque=true;
- 备注:当你的页面使用或继承了UINavigationController,那页面中this.NavigationController则不为空。
- 简介:主要用于页面的导航,可以用于跳转页面及页面使用导航条上的按钮。初次运行程序 加载UINavigationController
二、弹出UIPopoverController
- 简介:UIPopoverController主要有弹出窗口的作用。其作为容器,将要弹出的页面包含在其内部。
- 弹出代码:
barEvaluate.Clicked+=(sender,e)=>{ // 实例化要显示的UIViewController SystemSetView systemView=new SystemSetView(); if(popOverCtrl==null){ popOverCtrl=new UIPopoverController(systemView); //设置要弹出窗口的大小 popOverCtrl.PopoverContentSize=new SizeF(200f,500f); } //通过导航栏的按钮呈现 popOverCtrl.PresentFromBarButtonItem((UIBarButtonItem)sender,UIPopoverArrowDirection.Any,true); };
三、UIScrollView
- 简介:可以作为容器,进行页面的左右和上下滑动,支持缩放.
- 使用UIScrollView的代码,实现图片展的效果:
1 //实例化 2 var _scView = new UIScrollView(this.View.Frame); 3 this._scView.PagingEnabled = true; 4 this._scView.ShowsHorizontalScrollIndicator = false; 5 this._scView.ShowsVerticalScrollIndicator = false; 6 this._scView.Layer.BorderWidth = 2; 7 this._scView.Scrolled+=delegate{ 8 int curPage=((int)this._scView.ContentOffset.X+(int)LeftMarginIncrement/2)/(int)LeftMarginIncrement; 9 if(curPage==_currentPageIndex || curPage>20 ||curPage<0) 10 { 11 return; 12 } 13 _currentPageIndex=curPage; 14 }; 15 //设置要显示的内容 16 for(int i = 0; i <=20; i++) { 17 UIView myPictureView=new UIView(new RectangleF(ViewLeftMargin,ViewTopMargin,ViewWidth,ViewHeight)); 18 UIImageView myPictureImg=new UIImageView(new RectangleF(PicLeftMargin,PicTopMargin, PicWidth,PicHeight)); 19 myPictureImg.Image=UIImage.FromFile(string.Format("Resource/Img/{0}.jpg",i+1)); 20 myPictureView.AddSubview(myPictureImg); 21 this._pageViews.Add(myPictureView); 22 ViewLeftMargin=ViewLeftMargin+LeftMarginIncrement; 23 } 24 // 25 _scView.AddSubviews(_pageViews.ToArray()); 26 this.View = this._scView; 27 //设置需要显示的内容的大小 28 this._scView.ContentSize = new SizeF( _pageViews.Count* ViewWidth, ViewHeight ); 29 //设置当前显示的内容 根据内容的偏移量来定位 30 this._scView.ContentOffset = new PointF(_currentPageIndex * this.ViewWidth, 0);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述