iOS write a webview that can support fullscreen

Source: Internet
Author: User

So to write the layout

A titleview 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 margins and Superview remain the same _  textfield.enablesreturnkeyautomatically = YES;     Make return available at input _textfield.placeholder = @ "Please input:";  Set the value of hint _textfield.textalignment = nstextalignmentleft;   Text on the left shows _textfield.borderstyle = Uitextborderstylenone;   No Border _textfield.font = [Uifont systemfontofsize:18.0f];    Set Font Size _textfield.contenthorizontalalignment = Uicontrolcontenthorizontalalignmentcenter;  _textfield.contentverticalalignment = Uicontrolcontentverticalalignmentcenter;  These two make the text appear centered _textfield.clearbuttonmode = uitextfieldviewmodewhileediting; Clea when input is availableR button uiimage* image = [[UIImage imagenamed:@ "Text_field_bg.png"] stretchableimagewithleftcapwidth:20 topcapheight:   0]; Stretchableimagewithleftcapwidth make the picture have stretching effect _textfield.delegate = self;  Set proxy _textfield.background = image for TextField;    Background setting _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;            The above few words set the text with TextField's left some distance [self addsubview:_textfield]; uibutton* button = [UIButton buttonwithtype:uibuttontypecustom]; Init a button, you can customize the background [button setbackgroundimage:[[uiimage imagenamed:@ "Cancel_but_bg.png"]    Stretchableimagewithleftcapwidth:20 topcapheight:0] forstate:uicontrolstatenormal];  Button.frame = CGRectMake (260,10, 49, 30); Set button background with normal and highlight two states [button Setbackgroundimage:[[uiimage imagenamed:@ "Cancel_but_bg2.png"] streTchableimagewithleftcapwidth:20 topcapheight:0] forstate:uicontrolstatehighlighted];        [Button Settitlecolor:[ccommon rgbcolorfromhexstring:@ "#333333" alpha:1.0f] forstate:uicontrolstatenormal];    [Button Settitlecolor:[ccommon rgbcolorfromhexstring:@ "#000000" alpha:1.0f] forstate:uicontrolstatehighlighted];    [Button addtarget:self action: @selector (onclick) forcontrolevents:uicontroleventtouchupinside];    [Button settitle:@ "search" forstate:uicontrolstatenormal]; <strong> Button.autoresizingmask = uiviewautoresizingflexibleleftmargin;//This lets the button font periphery have the adaptive margin</        strong> [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} 


Viewcontroller's viewdidload:

-(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: [[CustomUi    Webviewuimanager 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];    <strong> nsarray* arr = [Self.customwidgetuiwebview subviews];    uiscrollview* Sview = [arr objectatindex:0];    [Sview Setopaque:no];    [Sview setbackgroundcolor:[uicolor colorwithpatternimage:[uiimage imagenamed:@ "Webview_bg.png"]]; This sets the opacity of the scrollview of the WebView sub view to opaque, plus the background sview.delegate = self;</strong> webtoolbarcontroller* TMPW Ebtoolbarcontroller = [[Webtoolbarcontroller alloc] init];//This is the bottom of 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; <strong> [self startupdatebuttonstatustimer]; This is a timer to update the back and forward status </strong> cgrect ICONRC = [[Customuiwebviewuimanager sharedinstance] geTfullscreenbtnframe];    uibutton* curbtn = [[UIButton alloc] INITWITHFRAME:ICONRC];    [Curbtn setimage:[uiimage imagenamed:@ "Fullscreen.png"] forstate:uicontrolstatenormal];    [Curbtn setimage:[uiimage imagenamed:@ "Fullscreen_touch.png"] forstate:uicontrolstatehighlighted];    [Curbtn addtarget:self Action: @selector (Fullscreenbtnclick:) forcontrolevents:uicontroleventtouchupinside];    Curbtn.alpha = 0;    SELF.FULLSCREENBTN = curbtn;      [Self.view ADDSUBVIEW:CURBTN]; }

-(void) startupdatebuttonstatustimer{[self freeupdatebuttonstatustimer];    nsdate* tmpdate = [[NSDate alloc] initwithtimeintervalsincenow:0.5]; nstimer* Tmptimer = [[Nstimer alloc] initwithfiredate:tmpdate interval:10.0 target:self selector: @selector (  updatebuttonstatustimerfired:) Userinfo:nil Repeats:yes]; <strong>//0.5s after execution <span style= "font-family:arial, Helvetica, Sans-serif;"    >updatebuttonstatustimerfired, and 10.0s re-execution </span></strong> self.updatestatusbartimer = Tmptimer; [[Nsrunloop Currentrunloop] Addtimer:_updatestatusbartimer formode:nsdefaultrunloopmode];} -(void) updatebuttonstatustimerfired: (ID) sender{[self updatetoobarbuttonstatusbywebview];//[self Freeupdatebuttonstatustimer];} -(void) updatetoobarbuttonstatusbywebview{if (![    _customwidgetuiwebview isloading]) {return;    } [_webtoolbarcontroller setitem:item_back enabled:[_customwidgetuiwebview CanGoBack]; [_webtoolbarcontroller Setitem:item_forward Enabled:[_customWidgetuiwebview CanGoForward]]; }

If you perform a full screen and leave 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 less than the screen height, the full-screen operation is not performed return; } if (self.customWidgetUiWebView.widgetEmbedStatus = = enowidgetembed) {NSLog (@ "====y=%f", Sview.contentoffset        . y); if (sview.contentoffset.y >= searchbar_height && Sview.contentoffset.y < SView.contentSize.height -screenheight) {//contentoffset<span class= "S1" style= "font-family:arial, Helvetica, Sans-serif;" > is the currently displayed area of the </span><span style= "font-family:arial, Helvetica, Sans-serif;" >origin</span><span class= "S1" style= "font-family:arial, Helvetica, Sans-serif;" > relative to the entire </span><span style= "font-family:arial, Helvetica, Sans-serif;" &gT;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;"            The position of > if it is greater than searchbar_height and less than the bottom </span> if (isfullscreenmode) {return;            }else{[self gotofullscreenmode];        }}else if (Isfullscreenmode) {[Self exitfullscreenmode];    }}}-(void) gotofullscreenmode{[UIView beginanimations:@ "Fullscreenmode" context:null];    [UIView setanimationduration:0.5f];    [UIView setanimationdelegate:self];        [UIView setanimationdidstopselector: @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] getwebviewframebyfullscreenview];    Shwo button [UIView beginanimations:@ "Animationid" context:null];    [UIView setanimationduration:0.5f];    _fullscreenbtn.alpha = 1;    [UIView commitanimations];    Isfullscreenmode = YES;    [_titleview Resigntextfield];    }-(void) exitfullscreenmode{[UIView beginanimations:@ "Notscreenmode" context:null];    [UIView setanimationduration:0.2f];    [UIView setanimationdelegate:self];    [UIView setanimationdidstopselector: @selector (animationDidStop:finished:context:)];    [UIView Setanimationbeginsfromcurrentstate:yes];        [UIView Setanimationcurve:uiviewanimationcurveeaseinout];                [Searchbarviewcontroller sharedinstance].view.frame = [[Searchbaruimanager sharedinstance] searchBarViewFrame]; _WebToolBarController.webToolBar.frame = [[Webtoolbaruimanager sharedinstance] webtoolbarframe]; if (!isbtnclickani) {_customwidgetuiwebview.frame = [[Customuiwebviewuimanager sharedinstance] GetWebViewFrameBySe        Archresultview];    _titleview.frame = [[Customuiwebviewuimanager sharedinstance] getsearchbarview];        } [UIView commitanimations];    [UIView beginanimations:@ "Animationid" context:null];    [UIView setanimationduration:0.5f];    _fullscreenbtn.alpha = 0;    [UIView commitanimations];    Isfullscreenmode = NO;    }-(void) Fullscreenbtnclick: (ID) Sender {//Isbtnclickani = YES;  [Self exitfullscreenmode]; Isbtnclickani = NO;}


Gosafari's 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

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.