Write a WebView that supports full screen in IOS

Source: Internet
Author: User

Write a WebView that supports full screen in IOS

In this way, write the Layout

A TitleView is used as the top search bar:

@ Implementation TitleView-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {// Initialization code [self initTilte];} return self;}-(void) initTilte {UITextField * field = [[UITextField alloc] initWithFrame: CGRectMake (20, 10,230, 30)]; _ textField = field; _ textField. autoresizingMask = UIViewAutoresizingFlexibleWidth; // automatically adjusts its position so that its left and right distances remain unchanged with superview _ textField. enablesReturnKeyAutomatically = YES; // enable return to use _ textField during input. placeholder = @ "please input:"; // sets the value of hint _ textField. textAlignment = NSTextAlignmentLeft; // text to the left: _ textField. borderStyle = UITextBorderStyleNone; // no border _ textField. font = [UIFont systemFontOfSize: 18366f]; // set the font size _ textField. contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; _ textField. contentverticalignment = UIControlContentVerticalAlignmentCenter; // these two enable the text to be centered and show _ textField. clearButtonMode = inputs; // when input, clear button UIImage * image = [[UIImage imageNamed: @ "text_field_bg.png"] stretchableImageWithLeftCapWidth: 20 topCapHeight: 0]; // stretchableImageWithLeftCapWidth makes the image stretch _ textField. delegate = self; // set proxy _ textField for textField. background = image; // set the background to textField. leftViewMode = UITextFieldViewModeAlways; [_ textField setText: @ "http://m.baidu.com"]; CGRect frame = [_ textField frame]; frame. size. width = 15; UIView * view1 = [[UIView alloc] initWithFrame: frame]; _ textField. leftView = view1; // set the distance between the text and textField on the left [self addSubview: _ textField]; UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom]; // init a button that can customize the background [button setBackgroundImage: [[UIImage imageNamed: @ "cancel_but_bg.png"] Priority: 20 topCapHeight: 0] forState: UIControlStateNormal]; button. frame = CGRectMake (, 10, 49, 30); // set the background of the button, which has two statuses: normal and highlight. [button setBackgroundImage: [UIImage imageNamed: @ "alarm"] Rule: 20 topCapHeight: 0] forState: UIControlStateHighlighted]; [button setTitleColor: [CCommon RGBColorFromHexString: @ "#333333" alpha: 1.0f] forState: UIControlStateNormal]; [button setTitleColor: [CCommon RGBColorFromHexString: @ "#000000" alpha: 1.0f] forState: Unknown]; [button addTarget: self action: @ selector (onclick) forControlEvents: UIControlEventTouchUpInside]; [button setTitle: @ "Search" forState: UIControlStateNormal];Button. autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; // This enables adaptive margin around the button font.[Self addSubview: button]; [button addTarget: self action: @ selector (onclick) forControlEvents: UIControlEventTouchUpInside]; UIView * view = [[UIView alloc] initWithFrame: CGRectMake (0, 44,320, 2)]; view. backgroundColor = [UIColor grayColor]; [self addSubview: view];}-(void) resignTextField {[_ textField resignFirstResponder];}-(void) onclick {[_ customUiWebViewController onclick]; // Let the controller perform the search page operation}


Viewdidload of viewcontroller:

-(Void) viewDidLoad {[super viewDidLoad]; _ titleView = [[TitleView alloc] initWithFrame: [[CustomUiWebViewUIManager sharedInstance] getSearchBarView]; _ titleView. customUiWebViewController = self; [self. view addSubview: _ titleView]; // Do any additional setup after loading the view. customWidgetUiWebView * tView = [[CustomWidgetUiWebView alloc] initWithFrame: [[CustomUiWebViewUIManager sharedInstance] getWebViewFrameBySearchResultView]; self. customWidgetUiWebView = tView; [self. customWidgetUiWebView setOpaque: NO]; [self. customWidgetUiWebView setBackgroundColor: [UIColor whiteColor]; [self. customWidgetUiWebView setDelegate: self]; self. customWidgetUiWebView. scalesPageToFit = YES; self. customWidgetUiWebView. detectsPhoneNumbers = NO; // NSURL * url = [NSURL URLWithString: @ "http://m.baidu.com"]; // NSURLRequest * request = [NSURLRequest requestWithURL: url]; // [self. customWidgetUiWebView loadRequest: request];NSArray * arr = [self. customWidgetUiWebView subviews]; UIScrollView * sView = [arr objectAtIndex: 0]; [sView setOpaque: NO]; [sView setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: @ "webview_bg.png"]; // sets the opacity of the scrollview of the sub view of the webview to opacity, and adds the background sView. delegate = self;WebToolBarController * tmpWebToolBarController = [[WebToolBarController alloc] init]; // This is the bottom toolbar self. webToolBarController = tmpWebToolBarController; [_ webToolBarController loadWebViewToolBar]; [self. view addSubview: _ customWidgetUiWebView]; _ webToolBarController. webToolBar. frame = [[WebToolBarUIManager sharedInstance] webToolBarFrame]; [self. view addSubview: _ webToolBarController. webToolBar]; self. webToolBarController. delegate = self;[Self startupdatebuttonstatustmer]; // a timer is initiated to update the status of back and forward.CGRect iconRc = [invalid sharedInstance] condition]; UIButton * curBtn = [[UIButton alloc] initWithFrame: iconRc]; [curBtn setImage: [UIImage imageNamed: @ "condition"] forState: UIControlStateNormal]; [curBtn setImage: [UIImage imageNamed: @ "success"] forState: Success]; [curBtn addTarget: self action: @ selector (fullScreenBtnClick :) forControlEvents: Success]; curBtn. alpha = 0; self. fullScreenBtn = curBtn; [self. view addSubview: curBtn];}

-(Void) startUpdateButtonStatusTimer {[self tuning]; NSDate * tmpDate = [[NSDate alloc] latency: 0.5]; NSTimer * tmpTimer = [[NSTimer alloc] initWithFireDate: tmpDate interval: 10.0 target: self selector: @ selector (UpdateButtonStatusTimerFired :) userInfo: nil repeats: YES];// Execute UpdateButtonStatusTimerFired after 0.5s, and execute it again after 10.0sSelf. updateStatusBarTimer = tmpTimer; [[nsunloop currentRunLoop] addTimer: _ updateStatusBarTimer forMode: NSDefaultRunLoopMode];}-(void) handle :( id) sender {[self timer]; // [self freeUpdateButtonStatusTimer];}-(void) updatemedibarbuttonstatusbywebview {if (! [_ Mwmwidgetuiwebview isLoading]) {return;} [_ webToolBarController setItem: ITEM_BACK enabled: [_ customWidgetUiWebView canGoBack]; [_ webToolBarController setItem: ITEM_FORWARD enabled: [_ mwmwidgetuiwebview canGoForward];}

If you perform the full screen and exit full screen operation:

-(Void) scrollViewDidEndDragging :( UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {CGFloat screenHeight = [UIScreen mainScreen]. applicationFrame. size. height; NSArray * arr = [self. customWidgetUiWebView subviews]; UIScrollView * sView = [arr objectAtIndex: 0]; if (sView. contentSize. height <screenHeight) {// if the content area of the webview is smaller than the screen height, return;} if (self. customWidgetUiWebView. widgetEmbedStat Us = ENoWidgetEmbed) {NSLog (@ "= y = % f", sView. contentOffset. y); if (sView. contentOffset. y> = SearchBar_Height & sView. contentOffset. y <sView. contentSize. height-screenHeight) {// contentOffset is the position of the origin of the currently displayed area relative to the origin of the entire scrollView. if it is greater than SearchBar_Height and less than the bottom if (isFullScreenMode) {return ;} else {[self goToFullScreenMode] ;}} else if (isFullScreenMode) {[self exitFullScreenMode] ;}}-(void) goT Detail {[UIView beginAnimations: @ "FullScreenMode" context: NULL]; [UIView setAnimationDuration: 0.5f]; [UIView attributes: self]; [UIView attributes: @ selector (animationDidStop: finished: context :)]; [UIView setAnimationBeginsFromCurrentState: YES]; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; _ webToolBarController. webToolBar. frame = [[WebToolBarUIManager SharedInstance] webToolBarHideFrame]; [UIView commitAnimations]; _ titleView. frame = [[CustomUiWebViewUIManager sharedInstance] getSearchBarViewFullView]; _ customWidgetUiWebView. frame = [[CustomUiWebViewUIManager sharedInstance] timeout]; // shwo button [UIView beginAnimations: @ "animationID" context: NULL]; [UIView setAnimationDuration: 0.5f]; _ fullScreenBtn. alpha = 1; [UIView c Response]; isFullScreenMode = YES; [_ titleView resignTextField];}-(void) Comment {[UIView beginAnimations: @ "NotScreenMode" context: NULL]; [UIView setAnimationDuration: 0.2f]; [UIView setAnimationDelegate: self]; [UIView setAnimationDidStopSelector: @ selector (animationDidStop: finished: context :)]; [UIView attributes: YES]; [UIView setAnimationCurve: UIViewAnimati OnCurveEaseInOut]; [SearchBarViewController sharedInstance]. view. frame = [[SearchBarUIManager sharedInstance] searchBarViewFrame]; _ webToolBarController. webToolBar. frame = [[WebToolBarUIManager sharedInstance] webToolBarFrame]; if (! IsBtnClickAni) {_ customWidgetUiWebView. frame = [[CustomUiWebViewUIManager sharedInstance] getWebViewFrameBySearchResultView]; _ titleView. frame = [[sharesharedinstance] getSearchBarView];} [UIView commitAnimations]; [UIView beginAnimations: @ "animationID" context: NULL]; [UIView setAnimationDuration: 0.5f]; _ blank. alpha = 0; [UIView commitAnimations]; isFullScreenMode = NO;}-(void) fullScreenBtnClick :( id) sender {// isBtnClickAni = YES; [self exitFullScreenMode]; // isBtnClickAni = NO ;}


Gosafari code:

- (void)goSafari{UIActionSheet *actionSheet = [[UIActionSheet alloc]  initWithTitle:NSLocalizedString(@"String_QueryOpBySafari", nil)  delegate:self  cancelButtonTitle:NSLocalizedString(@"String_Cancel", nil)  destructiveButtonTitle:nil  otherButtonTitles:NSLocalizedString(@"String_OpBySafari", nil),nil];[actionSheet showInView:self.view];}- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{if (buttonIndex==0){if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:[_customWidgetUiWebView stringByEvaluatingJavaScriptFromString:@"document.URL"]]]){[[CCommon sharedInstance ]appOpenUrl:[_customWidgetUiWebView stringByEvaluatingJavaScriptFromString:@"document.URL"]];}else{[[CCommon sharedInstance ]appOpenUrl:[[_customWidgetUiWebView.request URL]absoluteString]];}                }}


:


Code: http://download.csdn.net/detail/baidu_nod/7734661

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.