这样来写布局

一个TitleView作为顶部搜索栏:

  1. @implementation TitleView
  2.  
  3. - (id)initWithFrame:(CGRect)frame
  4. {
  5. self = [super initWithFrame:frame];
  6. if (self) {
  7. // Initialization code
  8. [self initTilte];
  9. }
  10. return self;
  11. }
  12.  
  13. -(void)initTilte{
  14. UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 230, 30)];
  15. _textField = field;
  16. _textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;//自己主动调整自己的位置,使自己的左边距和右边距和superview保持不变
  17. _textField.enablesReturnKeyAutomatically = YES; //使return在输入时能够使用
  18. _textField.placeholder = @"please input:"; //设置hint的值
  19. _textField.textAlignment = NSTextAlignmentLeft; //文字靠左显示
  20. _textField.borderStyle = UITextBorderStyleNone; //没有边框
  21. _textField.font = [UIFont systemFontOfSize:18.0f]; //设置字体大小
  22. _textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  23. _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; //这两个使文字会居中显示
  24. _textField.clearButtonMode = UITextFieldViewModeWhileEditing; //当输入时有clear button
  25. UIImage* image = [[UIImage imageNamed:@"text_field_bg.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:0]; //stretchableImageWithLeftCapWidth使图片有拉伸效果
  26. _textField.delegate = self; //给textField设置代理
  27. _textField.background = image; //背景设置
  28. _textField.leftViewMode = UITextFieldViewModeAlways;
  29. [_textField setText:@"http://m.baidu.com"];
  30. CGRect frame = [_textField frame];
  31. frame.size.width = 15;
  32. UIView* view1 = [[UIView alloc] initWithFrame:frame];
  33. _textField.leftView = view1; //上面几句话设置文字跟textField的左边有些距离
  34.  
  35. [self addSubview:_textField];
  36.  
  37. UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; //init一个button,能够自己定义背景
  38. [button setBackgroundImage:[[UIImage imageNamed:@"cancel_but_bg.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:0] forState:UIControlStateNormal];
  39. button.frame = CGRectMake(260,10, 49, 30); //设置button的背景。有normal和highlight两种状态
  40. [button setBackgroundImage:[[UIImage imageNamed:@"cancel_but_bg2.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:0] forState:UIControlStateHighlighted];
  41. [button setTitleColor:[CCommon RGBColorFromHexString:@"#333333" alpha:1.0f] forState:UIControlStateNormal];
  42.  
  43. [button setTitleColor:[CCommon RGBColorFromHexString:@"#000000" alpha:1.0f] forState:UIControlStateHighlighted]; [button addTarget:self action:@selector(onclick) forControlEvents:UIControlEventTouchUpInside];
  44. [button setTitle:@"搜索" forState:UIControlStateNormal];
  45. <strong> button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;//这个让button字体周边有自适应的margin</strong>
  46.  
  47. [self addSubview:button];
  48.  
  49. [button addTarget:self action:@selector(onclick) forControlEvents:UIControlEventTouchUpInside];
  50.  
  51. UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320,2)];
  52. view.backgroundColor = [UIColor grayColor];
  53. [self addSubview:view];
  54.  
  55. }
  56.  
  57. -(void)resignTextField{
  58. [_textField resignFirstResponder];
  59. }
  60.  
  61. -(void)onclick{
  62. [_customUiWebViewController onclick];//让controller运行搜索页面的操作
  63. }

viewcontroller的viewdidload:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4.  
  5. _titleView = [[TitleView alloc] initWithFrame:[[CustomUiWebViewUIManager sharedInstance] getSearchBarView]];
  6. _titleView.customUiWebViewController = self;
  7. [self.view addSubview:_titleView];
  8.  
  9. // Do any additional setup after loading the view.
  10. CustomWidgetUiWebView* tView = [[CustomWidgetUiWebView alloc] initWithFrame:
  11. [[CustomUiWebViewUIManager sharedInstance] getWebViewFrameBySearchResultView]];
  12. self.customWidgetUiWebView = tView;
  13.  
  14. [self.customWidgetUiWebView setOpaque:NO];
  15. [self.customWidgetUiWebView setBackgroundColor:[UIColor whiteColor]];
  16. [self.customWidgetUiWebView setDelegate:self];
  17. self.customWidgetUiWebView.scalesPageToFit = YES;
  18. self.customWidgetUiWebView.detectsPhoneNumbers = NO;
  19.  
  20. // NSURL* url = [NSURL URLWithString:@"http://m.baidu.com"];
  21. // NSURLRequest* request = [NSURLRequest requestWithURL:url];
  22. // [self.customWidgetUiWebView loadRequest:request];
  23.  
  24. <strong> NSArray* arr = [self.customWidgetUiWebView subviews];
  25. UIScrollView* sView = [arr objectAtIndex:0];
  26. [sView setOpaque:NO];
  27. [sView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"webview_bg.png"]]];
  28. //这个把webview的sub view的scrollview的透明度设为不透明,并加上背景
  29. sView.delegate = self;</strong>
  30.  
  31. WebToolBarController* tmpWebToolBarController = [[WebToolBarController alloc] init];//这个是底下的toolbar
  32. self.webToolBarController = tmpWebToolBarController;
  33.  
  34. [_webToolBarController loadWebViewToolBar];
  35. [self.view addSubview:_customWidgetUiWebView];
  36. _webToolBarController.webToolBar.frame = [[WebToolBarUIManager sharedInstance] webToolBarFrame];
  37.  
  38. [self.view addSubview:_webToolBarController.webToolBar];
  39. self.webToolBarController.delegate = self;
  40.  
  41. <strong> [self startUpdateButtonStatusTimer]; //这个是起了一个timer来更新back和forward的状态</strong>
  42.  
  43. CGRect iconRc = [[CustomUiWebViewUIManager sharedInstance] getFullScreenBtnFrame];
  44. UIButton* curBtn = [[UIButton alloc] initWithFrame:iconRc];
  45. [curBtn setImage:[UIImage imageNamed:@"fullScreen.png"] forState:UIControlStateNormal];
  46. [curBtn setImage:[UIImage imageNamed:@"fullScreen_touch.png"] forState:UIControlStateHighlighted];
  47. [curBtn addTarget:self action:@selector(fullScreenBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  48. curBtn.alpha = 0;
  49. self.fullScreenBtn = curBtn;
  50. [self.view addSubview:curBtn];
  51.  
  52. }
  1. -(void)startUpdateButtonStatusTimer{
  2. [self freeUpdateButtonStatusTimer];
  3. NSDate* tmpDate = [[NSDate alloc] initWithTimeIntervalSinceNow:0.5];
  4. NSTimer* tmpTimer = [[NSTimer alloc] initWithFireDate:tmpDate interval:10.0 target:self selector:@selector(UpdateButtonStatusTimerFired:) userInfo:nil repeats:YES]; <strong>//0.5s以后运行<span style="font-family: Arial, Helvetica, sans-serif;">UpdateButtonStatusTimerFired,而且10.0s再反复运行</span></strong>
  5.  
  6. self.updateStatusBarTimer = tmpTimer;
  7. [[NSRunLoop currentRunLoop] addTimer:_updateStatusBarTimer forMode:NSDefaultRunLoopMode];
  8. }
  9.  
  10. - (void)UpdateButtonStatusTimerFired:(id)sender
  11. {
  12. [self updateToobarButtonStatusByWebView];
  13. //[self freeUpdateButtonStatusTimer];
  14. }
  15.  
  16. - (void)updateToobarButtonStatusByWebView{
  17. if (![_customWidgetUiWebView isLoading]) {
  18. return;
  19. }
  20.  
  21. [_webToolBarController setItem:ITEM_BACK enabled:[_customWidgetUiWebView canGoBack]];
  22. [_webToolBarController setItem:ITEM_FORWARD enabled:[_customWidgetUiWebView canGoForward]];
  23.  
  24. }

假设运行全屏和离开全屏的操作:

  1. -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
  2. CGFloat screenHeight = [UIScreen mainScreen].applicationFrame.size.height;
  3. NSArray* arr = [self.customWidgetUiWebView subviews];
  4.  
  5. UIScrollView* sView = [arr objectAtIndex:0];
  6. if (sView.contentSize.height < screenHeight) { //假设webview的内容区域小于屏幕高度,则不运行全屏操作
  7. return;
  8. }
  9.  
  10. if (self.customWidgetUiWebView.widgetEmbedStatus == ENoWidgetEmbed){
  11. NSLog(@"====y=%f",sView.contentOffset.y);
  12. if (sView.contentOffset.y >= SearchBar_Height &&
  13. sView.contentOffset.y < sView.contentSize.height-screenHeight) {//contentOffset<span class="s1" style="font-family: Arial, Helvetica, sans-serif;">是当前显示的区域的</span><span style="font-family: Arial, Helvetica, sans-serif;">origin</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;">相对于整个</span><span style="font-family: Arial, Helvetica, sans-serif;">scrollView</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;">的</span><span style="font-family: Arial, Helvetica, sans-serif;">origin</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;">的位置,假设大于SearchBar_Height,而且小于底部
  14. </span>
  15.  
  16. if (isFullScreenMode) {
  17. return;
  18. }else{
  19. [self goToFullScreenMode];
  20. }
  21. }else if(isFullScreenMode) {
  22. [self exitFullScreenMode];
  23. }
  24.  
  25. }
  26. }
  27.  
  28. -(void)goToFullScreenMode{
  29. [UIView beginAnimations:@"FullScreenMode" context:NULL];
  30. [UIView setAnimationDuration:0.5f];
  31. [UIView setAnimationDelegate:self];
  32. [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
  33.  
  34. [UIView setAnimationBeginsFromCurrentState:YES];
  35. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  36.  
  37. _webToolBarController.webToolBar.frame = [[WebToolBarUIManager sharedInstance] webToolBarHideFrame];
  38. [UIView commitAnimations];
  39.  
  40. _titleView.frame =[[CustomUiWebViewUIManager sharedInstance] getSearchBarViewFullView];
  41.  
  42. _customWidgetUiWebView.frame = [[CustomUiWebViewUIManager sharedInstance] getWebViewFrameByFullScreenView];
  43.  
  44. //shwo button
  45. [UIView beginAnimations:@"animationID" context:NULL];
  46. [UIView setAnimationDuration:0.5f];
  47. _fullScreenBtn.alpha = 1;
  48. [UIView commitAnimations];
  49. isFullScreenMode = YES;
  50. [_titleView resignTextField];
  51.  
  52. }
  53.  
  54. -(void)exitFullScreenMode{
  55. [UIView beginAnimations:@"NotScreenMode" context:NULL];
  56. [UIView setAnimationDuration:0.2f];
  57. [UIView setAnimationDelegate:self];
  58. [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
  59. [UIView setAnimationBeginsFromCurrentState:YES];
  60. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  61.  
  62. [SearchBarViewController sharedInstance].view.frame =
  63. [[SearchBarUIManager sharedInstance] searchBarViewFrame];
  64.  
  65. _webToolBarController.webToolBar.frame = [[WebToolBarUIManager sharedInstance] webToolBarFrame];
  66. if (!isBtnClickAni) {
  67. _customWidgetUiWebView.frame = [[CustomUiWebViewUIManager sharedInstance] getWebViewFrameBySearchResultView];
  68. _titleView.frame = [[CustomUiWebViewUIManager sharedInstance] getSearchBarView];
  69.  
  70. }
  71. [UIView commitAnimations];
  72.  
  73. [UIView beginAnimations:@"animationID" context:NULL];
  74. [UIView setAnimationDuration:0.5f];
  75. _fullScreenBtn.alpha = 0;
  76. [UIView commitAnimations];
  77. isFullScreenMode = NO;
  78.  
  79. }
  80.  
  81. - (void)fullScreenBtnClick:(id)sender {
  82. // isBtnClickAni = YES;
  83. [self exitFullScreenMode];
  84. // isBtnClickAni = NO;
  85. }

gosafari的代码:

  1. - (void)goSafari
  2. {
  3. UIActionSheet *actionSheet = [[UIActionSheet alloc]
  4. initWithTitle:NSLocalizedString(@"String_QueryOpBySafari", nil)
  5. delegate:self
  6. cancelButtonTitle:NSLocalizedString(@"String_Cancel", nil)
  7. destructiveButtonTitle:nil
  8. otherButtonTitles:NSLocalizedString(@"String_OpBySafari", nil),nil];
  9. [actionSheet showInView:self.view];
  10.  
  11. }
  12.  
  13. - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
  14. {
  15. if (buttonIndex==0)
  16. {
  17. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:[_customWidgetUiWebView stringByEvaluatingJavaScriptFromString:@"document.URL"]]])
  18. {
  19. [[CCommon sharedInstance ]appOpenUrl:[_customWidgetUiWebView stringByEvaluatingJavaScriptFromString:@"document.URL"]];
  20. }
  21. else
  22. {
  23. [[CCommon sharedInstance ]appOpenUrl:[[_customWidgetUiWebView.request URL]absoluteString]];
  24. }
  25.  
  26. }
  27. }

效果图:

代码:http://download.csdn.net/detail/baidu_nod/7734661

IOS写一个能够支持全屏的WebView的更多相关文章

  1. 使用AVPlayer自定义支持全屏的播放器(五)—Swift重构版本

    前言 很早之前开源了一个简单的视频播放器,由于年久失修,效果惨目忍睹,最近特意花时间对其进行了深度重构.旧版本后期不再维护,新版本使用Swift实现,后续会增加更多功能.不想看文字的请自行下载代码-- ...

  2. iOS 实现单个页面支持横竖屏,其他页面只能竖屏

    最近在自己的项目里面 有需要做一个需求 : app中某一个页面支持横竖屏, 而其他页面只能竖屏. 1 2 实现方法如下: 1 首先需要Xcode中选中支持的屏幕方向  2 Appdelegate中 . ...

  3. 如何制作一个完美的全屏视频H5

    写在前面的话: 最近一波H5广告火爆整个互联网圈,身为圈内人,我们怎能     不! 知!道! :( 嘘!真不知道的也继续看下去,有收获 ↓ ) So,搞懂这个并不难. 这篇文章将带你从头到尾了解H5 ...

  4. iOS端一次视频全屏需求的实现(转)

    对于一个带有视频播放功能的app产品来说,视频全屏是一个基本且重要的需求.虽然这个需求看起来很简单,但是在实现上,我们前后迭代了三套技术方案.这篇文章将介绍这三种实现方案中的利弊和坑点,以及实现过程中 ...

  5. ios单独的页面支持横竖屏的状态调整,HTML5加载下(更新2)

    单独的页面支持横竖屏的状态调整,HTML5加载下 工程中设置只支持竖屏状态,在加载HTML5的界面可以是横竖屏的,在不对工程其他界面/设置做调整的同时,可以这样去 #import "View ...

  6. 手动写一个类支持foreach循环

    之前初学时看过可以实现Iterable接口实现Iterator迭代器的支持,并且也支持foreach循环.现在学习了数据结构,手动写一个单链表支持foreach循环吧. 手写foreach循环步骤: ...

  7. iOS开发——点击图片全屏显示

    点击图片,全屏显示,然后再点击屏幕一下,返回. 没啥难的,直接上代码: // //  ViewController.m //  Demo-hehe // //  Created by yyt on 1 ...

  8. iOS 全屏侧滑/UIScrollView/UISlider间滑动冲突

    代码地址如下:http://www.demodashi.com/demo/13848.html 效果预览 一.前期准备 有一个支持全屏侧滑返回的视图控制器ViewController,ViewCont ...

  9. iOS 视频全屏功能 学习

    项目中,也写过类似"视频全屏"的功能, 前一阵子读到今日头条 的一篇技术文章,详细介绍三种旋转方法差异优劣最终择取.文章从技术角度看写的非常好,从用户角度看,也用过多家有视频功能的 ...

随机推荐

  1. [CSharp] C#开源大全

    商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...

  2. [牛客网练习赛 45 F] Magic Slab 解题报告 (最大权闭合子图)

    interlinkage: https://ac.nowcoder.com/acm/contest/847/F description: solution: 最大权闭合子图; 每个单元格看成一个正权点 ...

  3. UVA-12578 10:6:2 计算几何 模拟

    题面 题意:给你一块长方形,告诉你长:宽=10:6 里面有一个圆,长:半径=5:1,给你长方形的长,求圆的面积和剩余部分的面积 题解:直接模拟输出就好 #include<bits/stdc++. ...

  4. MongoDB Master-Slave cluster with authentication setup

    Master Server create mongo db folder with sub folders like data, conf, && log mkdir -p /opt/ ...

  5. 在ubuntu中安装Markdown神器Typora

    title: 在ubuntu中安装Markdown神器Typora toc: false date: 2018-09-01 17:48:15 categories: methods tags: ubu ...

  6. Ubuntu16.04下将hadoop2.7.3源代码导入到eclipse neon中

    0.为什么会有这篇: 这篇文章的目的在于帮助想学习hadoop源码的内容,却在导入的过程中出现了各种问题的人. 或许你一定找了很多博客都无果,那么不用担心,我和你一样,这也是这篇文章存在的意义,废话少 ...

  7. MFC知识点总结

    一:消息1.什么是消息?消息是驱动windows系统运行的基础.从计算机的角度来看,消息就是一个整数.    (1)一个无符号整数,是消息值:    (2)消息附带的WPARAM和LPARAM类型的参 ...

  8. MemCached总结一:Unbutu操作系统下memcached服务器安装和telnet方式连接memcache

    1.在Unbutu上安装memcached服务器 sudo apt-get update sudo apt-get install memcached 2. 确认memcached是否安装 要确认me ...

  9. 阿里云API网关!

    API 网关(API Gateway)提供高性能.高可用的 API 托管服务,帮助用户对外开放其部署在 ECS.容器服务等阿里云产品上的应用,提供完整的 API 发布.管理.维护生命周期管理.用户只需 ...

  10. 路飞学城Python-Day186

    Evernote Export 持续集成 持续集成,简单的说就是持续集成频繁的将代码集成到主干,它的好处主要有1.快速发现错误,没完成一点更新,就集成到主干,可以快速发现错误,定位错误也会比较容易,2 ...