我們在寫程序的時候,屏幕的寬高我們不能寫固定,例如寫成320,480之類的,因為我們要適配不同的手機。常用的一種方法是在 .pch文件中設置寬高,因為.pch是一個預編譯文件,在全局都能訪問到。
#define ScreenHeight [UIScreen mainScreen].bounds.size.hight;
#define ScreenWidth [UIScreen mainScreen].bounds.size.width;
這個問題比較簡單,完全算不上技巧,但是有時我們會忽略這個用法。就是給UIView設置背景圖片。
_tabbarView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background.png"]];
我們的圖片是方形的,我們想讓它以圓形或者橢圓類似的形狀在界面上顯示。
_image = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,80,80)];
_image.layer.cornerRadius = 40;//大小不同,形狀不同
_image.clipsToBounds = YES;
如何獲取主窗口的三種方法。
UIapplication *app=[UIApplication shareApplication];
1,UIWindow *window = app.keyWindow;
2,UIWindow *window = app.delegate.window;
3,UIWindow *window = [app.windows objectAtIndex:0];
當然我們也可以使用連寫的方法:比如:UIWindow *window = [UIApplication shareApplication].delegate.window;
我們有時候不小心修改了系統的API,或者其它的一些操作,出現了以下問題該怎么辦?
錯誤信息:fatal error :file /Application ……….
Note: after modifying system headers, please delete the module cache at /Users …..
解決辦法:前往/Users….這個文件夾,刪除里面的文件,然后clean一下程序即可解決.
我們如何讓定時器暫停和開始,并不是開啟和關閉。
NSTimer *timer = [NSTimer sch……];
[timer setFireDate :[NSDate distantFuture]];//暫停
[timer setFireDate :[NSDate distantPass]]//開始
我們彈出的鍵盤通常都是字母鍵盤,如何彈出數字鍵盤。
Textfiled.keyboardType = UIKeyboardTypeNumberPad;
在網絡請求中,如果請求體中有漢字的話,請求會出錯,怎樣解決?
假設請求體為:NSString *urlString= @“http://v.juhe.cn/weather/index?cityname=北京”;
因為有漢字字符,請求會出錯。處理辦法:
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
如何讓我們的圖片放大不變形。
_image = [[UIImage imageNamed:@"a.png"]stretchableImageWithLeftCapWidth:20 topCapHeight:10];
ARC和非ARC混編。
如果你的項目中使用的是非ARC,導入的ARC代碼的文件,需要在ARC模式的文件代碼中加入-fobjc-arc標簽
如果你的項目中使用的是ARC模式,則為非ARC模式的代碼文件加入-fno-objc-arc標簽
添加標簽方法:target->build phases->compile sources ,雙擊對應的 .m文件,在彈出框中輸入標簽。
在自定義xib中cell的時候,UIImageView的圖形跟定義的大小不匹配,可能按照原圖片顯示。
可能出現的問題是:UIImageView的命名為imageView,這個名字和系統的名字沖突,圖片不能按預期的顯示。
uitableview代理不調用。
常見的是沒有設置代理。
還有一種情況是繼承錯誤,一般來說是繼承UIViewController,如果繼承關系寫錯,代理方法不會調用。我當時還出現了一下錯誤。
Two-stage rotation animation is dePRecated. This application should use the smoother single-stage animation
如何找到xib上的view,可能有多個,可以用數組接收。
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomView" owner:self options:nil];
//得到第一個UIView
UIView *tmpCustomView = [nib objectAtIndex:0];
CGRect rect = [str boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
在UIButton上添加一個UIView,會使得UIButton的點擊事件不能響應,解決辦法是將UIView的可交互性改為NO.
如果界面中有ScrollView,并且界面中有導航條,視圖控制器會默認給scrollview設置一個64偏移。
我們可以使用self.automaticallAdjustsScrollViewInsets = NO,scrollview不會偏移。
tabbar高度49,導航加狀態欄高度64.
NSLog(@"%s",__FUNCTION__);
//字面量基本寫法:
// NSString *str = @"123";
// NSDictionary *dict = @{@"name":@"wyg",@"age":@"12",@"sex":@"boy"};
// NSArray *arr = @[@"1",@"2"];
// NSString *st = arr[1];
// NSNumber *intNumber = @123;
// NSNumber *boolNumber = @NO;
新聞熱點
疑難解答