iOS mark list

  1. [[UIScreen mainScreen] scale]:  计算屏幕分辨率  

       [[UIScreen main] scale] == 1; //代表320 x 480 的分辨率

       [[UIScreen main] scale] == 2; //代表640 x 960 的分辨率。  4s

       [[UIScreen main] scale] == 3; //代表1242 x 2208 的分辨率

 2. 检测应用内是否安装某个应用

 NSBundle *container = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/MobileContainerManager.framework"];

    if ([container load]) {

        Class appContainer = NSClassFromString(@"MCMAppContainer");

        id test = [appContainer performSelector:@selector(containerWithIdentifier:error:) withObject:@"com.tencent.mqq11" withObject:nil];

        NSLog(@"%@",test);

        if (test) {

           NSLog(@"yes");

        } else {

           NSLog(@"no");

        }

    }

或者用[[uiapplication shareapplication]openUrl:url];

 3. 输入框限制输入长度

     实现代理方法:

- (void)textViewDidChange:(UITextView *)textView{

     NSString *toBeString = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    //获取高亮部分

    UITextRange *selectedRange = [textView markedTextRange];

    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];

    

    // 没有高亮选择的字,则对已输入的文字进行字数统计和限制

    if (!position)

    {

        if (toBeString.length > MAX_STARWORDS_LENGTH)

        {

            [MBProgressHUD showAlert:@"最多只能输入150个字。"];

            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];

            if (rangeIndex.length == 1)

            {

                textView.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];

            }

            else

            {

                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];

                textView.text = [toBeString substringWithRange:rangeRange];

            }

        }

    }

}

4.点击html中的图片,要求能够对该图片放大缩小

    1》在webview的代理方法webViewDidFinishLoad中注入js

    NSString *js = @"function addImgClickEvent() {

    var imgs = document.getElementsByTagName('img');

    for (var i = 0; i < imgs.length; ++i) {

    var img = imgs[i];

    img.onclick = function () {

    window.location.href = 'hyb-image-preview:' + this.src;

    };

    }

    }";

    // 注入JS代码

    [webView stringByEvaluatingJavaScriptFromString:js];

    // 执行所注入的JS代码

    [webView stringByEvaluatingJavaScriptFromString:@"addImgClickEvent();"];

   2》实现webview的代理方法

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    NSLog(@"request.URL:--------%@",request.URL);

    if ([request.URL.scheme hasPrefix:@"hyb-image-preview"]) {

        // 获取原始图片的完整URL

        NSString *src = [request.URL.absoluteString stringByReplacingOccurrencesOfString:@"hyb-image-preview:" withString:@""];

        if (src.length > 0) {

            [self showImageWithPath:src];

            //[self showImageWithURL:src];

        }

    }

    return YES;

}

5.对webview进行缩放

        _contentWebView.scalesPageToFit = YES;

        _contentWebView.multipleTouchEnabled = YES;

        _contentWebView.userInteractionEnabled = YES;

        _contentWebView.scrollView.scrollEnabled = YES;

        _contentWebView.contentMode = UIViewContentModeScaleAspectFit;

然后在webViewDidFinishLoad中注入js

      NSString *jsMeta = [NSString stringWithFormat:@"var meta = document.createElement('meta');meta.content='width=device-width,initial-scale=1.0,minimum-scale=0.5,maximum-scale=3';meta.name='viewport';document.getElementsByTagName('head')[0].appendChild(meta);"];    

    [webView stringByEvaluatingJavaScriptFromString:jsMeta];

 6. 判断某个点是否在某区域内

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

        CGPoint point = [[touches anyObject] locationInView:self.view];

        BOOL sure= CGRectContainsPoint(_bigView.frame, point);

        if (!sure) {

        // 判断点击的区域如果不是pageController, 则关闭弹框

         for (UIView *view in [AppDelegate sharedInstanced].window.subviews) {

            if (view.tag == 1230) {

                [view removeFromSuperview];

                break;

            }

        }

    }

}

7. 在各种view中获取父类控制器的方法

- (UIViewController *)getCurrentVC {

    

    UIWindow * window = [[UIApplication sharedApplication] keyWindow];

    

    if (window.windowLevel != UIWindowLevelNormal){

        

        NSArray *windows = [[UIApplication sharedApplication] windows];

        

        for(UIWindow * tmpWin in windows){

            

            if (tmpWin.windowLevel == UIWindowLevelNormal){

                window = tmpWin;

                break;

            }

        }

    }

    

    UIViewController *result = window.rootViewController;

    while (result.presentedViewController) {

        result = result.presentedViewController;

    }

    if ([result isKindOfClass:[UITabBarController class]]) {

        result = [(UITabBarController *)result selectedViewController];

    }

    

    if ([result isKindOfClass:[UINavigationController class]]) {

        result = [(UINavigationController *)result topViewController];

    }

    return result;    

}

 8: searchbar 问题

 searchBar.searchTextPositionAdjustment = UIOffsetMake(10, 0);  //设置文字偏移量

9. ipad 中tabbar 文字图片排列问题

     新建类: mytabbar。继承 UITabbar ,视线如下方法

- (UITraitCollection *)traitCollection {

    

    if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {

        

        return [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassCompact];

        

    }

    

    return [super traitCollection];

    

}

将系统tabbar 替换成mytabbar

  MyTabBar *tabBar = [[MyTabBar alloc] initWithFrame:self.tabBar.frame];

    tabBar.backgroundColor = [UIColor whiteColor];

    //设置tabbar时只能用keyValue方式

    [self setValue:tabBar forKeyPath:@"tabBar"];

9 : 定时器 

a>子线程启动定时器

  dispatch_async(dispatch_get_global_queue(0, 0), ^{

        weakSelf.timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(autoScrollAction) userInfo:nil repeats:YES];

        [[NSRunLoop currentRunLoop] addTimer: weakSelf.timer forMode:NSRunLoopCommonModes];

        [[NSRunLoop currentRunLoop] run];

    });

b> 主线程刷新

     dispatch_async(dispatch_get_main_queue(), ^{

      // do something

    });

原文地址:https://www.cnblogs.com/Cyan-zoey/p/9228830.html