[[UIScreen main] scale] == 1; //表明320 x 480 的分辨率html
[[UIScreen main] scale] == 2; //表明640 x 960 的分辨率。 4sweb
[[UIScreen main] scale] == 3; //表明1242 x 2208 的分辨率windows
NSBundle *container = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/MobileContainerManager.framework"];app
if ([container load]) {async
Class appContainer = NSClassFromString(@"MCMAppContainer");oop
id test = [appContainer performSelector:@selector(containerWithIdentifier:error:) withObject:@"com.tencent.mqq11" withObject:nil];ui
NSLog(@"%@",test);this
if (test) {lua
NSLog(@"yes");url
} else {
NSLog(@"no");
}
}
或者用[[uiapplication shareapplication]openUrl:url];
實現代理方法:
- (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];
}
}
}
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;
}
_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
});