zoukankan      html  css  js  c++  java
  • 阶段性总结

    写了几天的代码,感觉确实有不少提升。目前基本的UI界面已经搭建的差不多了,但更高级的功能,比如收藏夹,夜间模式,页內搜索,关闭图片都还没有实现。

    今天决定总结一下这几天的收获,方便以后查阅。

    1.Storyboard 和 self.view 的关系

    Storyboard中可以添加控件,self.view也可以做添加控件。那他们一样么?

    我在搭建浏览器UI界面时发现:Storyboard添加的控件处于最底层,利用self.view执行_addSubview:_而得到的控件是覆盖在Storyboard之上的。

    2.按键功能的代码实现

    在Storyboard中,添加按钮和实现IBAction功能是一件很惬意的事情。鼠标拖拖拽拽,添加几个名称就搞定了。但是如果按钮是用代码_initWithFrame:_实现的呢?那怎样给按钮添加功能呢?

    答案是:

    [“按键名称” addTarget:self action:@selector( “方法名称” ) forControlEvents:UIControlEventTouchUpInside];
    

    3.页面跳转的代码实现

    和上一条情况类似。

    首先得指定Storyboard和其中的对应UIViewController。在Storyboard中设置好对应UIViewController的StoryboardID。

    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *targetViewController = [storyBoard instantiateViewControllerWithIdentifier:@"targetViewID"];
    [self showViewController:targetViewController sender:self];
    

    4.页面与键盘的随动功能

    我的浏览器将地址栏设在屏幕下方,利用self.view执行_addSubview:_会出现一个bug:点击地址栏弹出的键盘将地址栏给覆盖了,只能盲输。完全不能忍!

    Stackoverflow给出了一个很不错的方法:基于苹果官方的代码 ,并作了部分修改。
    http://stackoverflow.com/a/4837510/5243422
    https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7

    这一方案是首先在self.view上添加一个UIScrollView,将WkWebview添加在UIScrollView。只有UIScrollView可以上下移动,其子类有UITableView,UICollectionView等等。

    然后在建立self.view的位置执行第一段代码,监听键盘的动作,并执行相应的对策。

    #第一段代码

    // Call this method somewhere in your view controller setup code.
    
    - (void)registerForKeyboardNotifications {
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    }
    

    #第二段代码

    // Called when the UIKeyboardDidShowNotification is sent.
    
    - (void)keyboardWasShown:(NSNotification*)aNotification {
    /*
    //苹果官方代码 
     NSDictionary* info = [aNotification userInfo];
     CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
     UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
     self.scrollView.contentInset = contentInsets;
     self.scrollView.scrollIndicatorInsets = contentInsets;
    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
     CGRect aRect = self.view.frame;
     aRect.size.height -= kbSize.height;
     if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
               [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
     }
    */
    
    //StackOverFlow代码 
     NSDictionary* info = [aNotification userInfo];
     CGRect keyPadFrame=[[UIApplication sharedApplication].keyWindow convertRect:[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] fromView:self.view];
     CGSize kbSize =keyPadFrame.size;
     CGRect activeRect=[self.view convertRect:self.urlTextField.frame fromView:self.urlTextField.superview];
     CGRect aRect = self.view.bounds;
     aRect.size.height -= (kbSize.height);
     CGPoint origin = activeRect.origin;
     origin.y -= self.scrollView.contentOffset.y;
    
     if (!CGRectContainsPoint(aRect, origin)) {
                CGPoint scrollPoint = CGPointMake(0.0,CGRectGetMaxY(activeRect)-(aRect.size.height));
                [self.scrollView setContentOffset:scrollPoint animated:YES];
      }
    
    }
    

    #第三段代码

    // Called when the UIKeyboardWillHideNotification is sent
    
     - (void)keyboardWillBeHidden:(NSNotification*)aNotification {
    
     /* 原代码没用,只是修改页边距,没有执行
     UIEdgeInsets contentInsets = UIEdgeInsetsZero;
     self.scrollView.contentInset = contentInsets;
     self.scrollView.scrollIndicatorInsets = contentInsets;
     */
     //我的方案
     [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    
    }
    

    5.进度条的加载
    WkWebview原生提供了API _estimatedProgress_可以输出当前的加载进度,但是直接执行_setProgress: animated:_方法是没有用的。

    Stackoverflow给出了解决方案
    http://stackoverflow.com/q/26198334/5243422

    首先给self.webView添加观察事件。

    [self.webView addObserver:self forKeyPath:@"estimatedProgress"
                                      options:NSKeyValueObservingOptionNew
                                      context:NULL];
    

    然后执行下列方法
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

      if ([keyPath isEqualToString:@"estimatedProgress"] && object == self.webView) {
           [self.loadingProgress setAlpha:1.0f];
           [self.loadingProgress setProgress:self.webView.estimatedProgress animated:YES];
           if(self.webView.estimatedProgress >= 1.0f) {
                      [UIView animateWithDuration:0.3 
                                            delay:0.3
                                         options:UIViewAnimationOptionCurveEaseOut
                                       animations:^ {
                             [self.loadingProgress setAlpha:0.0f];
                             }
                                       completion:^(BOOL finished) {
                             [self.loadingProgress setProgress:0.0f animated:NO];
                             }
                      ];
          }
     }else {
           [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
     }
    
    }
    

    这一段是内存管理

    - (void)dealloc {
    
     [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
     // if you have set either WKWebView delegate also set these to nil here
     [self.webView setNavigationDelegate:nil];
     [self.webView setUIDelegate:nil];
    
    }
    

    6、如何改变PickerView中的文字颜色

    UIPickerView中不能直接设置文字格式,只能使用属性字符串。

    - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
      NSAttributedString *attString = [[NSAttributedString alloc] initWithString:self.searchEnginesList[row] attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
      return attString;
    
    }
  • 相关阅读:
    <转> 解决异常:IllegalStateException: Fragment <ThisFragment> is not currently in the FragmentManager
    带你走进Android Afinal框架的世界
    Android滑动菜单特效实现,仿人人客户端侧滑效果,史上最简单的侧滑实现
    Myeclipse启动不了的解决方法
    select 禁止 选择
    jquery的clone方法应用于textarea和select的bug修复不能copy值,clone id重复的解决
    玩javaweb的web.xml编译路径
    程序员笔记之设计(一)
    Eclipse清除SVN密码
    Java 最简单的批处理
  • 原文地址:https://www.cnblogs.com/rim99/p/5009883.html
Copyright © 2011-2022 走看看