仿網(wǎng)易彩票代碼實現(xiàn)
來源:程序員人生 發(fā)布時間:2014-12-13 08:44:07 閱讀次數(shù):3929次
仿網(wǎng)易彩票代碼實現(xiàn)
1.設(shè)置全部導(dǎo)航條的背景
//
取出全部導(dǎo)航條
UINavigationBar
*bar = [UINavigationBar
appearance];
//
設(shè)置全部導(dǎo)航條的背景圖片
[bar setBackgroundImage:[UIImage imageName:
@"navigationbar_background.png"] forBarMetrics:UIBarMetricsDefault];
//
導(dǎo)航欄上有1層BackgroundImageView,不能直接設(shè)置背景色彩,設(shè)置背景色彩是無效的
// bar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"navigationbar_background.png"]];
//
設(shè)置所有導(dǎo)航條字體的色彩
NSDictionary
*dict =
@{NSFontAttributeName: [UIFont
systemFontOfSize:15.0],NSForegroundColorAttributeName:[UIColor
whiteColor]};
[bar setTitleTextAttributes:dict];
//
設(shè)置主題色彩
[bar setTintColor:[UIColor
whiteColor]];
2、解決IOS6和IOS7兼容性問題
程序啟動的時候,隱藏狀態(tài)欄,ios6需要恢復(fù)狀態(tài)欄顯示
設(shè)置狀態(tài)欄色彩 ios7默許狀態(tài)欄交給控制器管理,修改info.plist文件,讓狀態(tài)欄交給application管理
application.statusBarHidden
=
NO;
application.statusBarStyle
=
UIStatusBarStyleLightContent;
3、自定義button,設(shè)置button的標(biāo)題和圖片的位置
//
設(shè)置按鈕標(biāo)題的位置
- (CGRect)titleRectForContentRect:(CGRect)contentRect;
//
設(shè)置按鈕圖片的位置
- (CGRect)imageRectForContentRect:(CGRect)contentRect;
// 獲得當(dāng)前文字尺寸,計算內(nèi)部Label的尺寸
NSDictionary
*dict =
@{NSFontAttributeName: [UIFont
systemFontOfSize:15.0]};
titleW =
[self.currentTitle
boundingRectWithSize:CGSizeMake(MAXFLOAT,
MAXFLOAT)
options:NSStringDrawingTruncatesLastVisibleLine
attributes:dict
context:nil].size.width;
注意:IOS6和IOS7有兼容性問題
boundingRectWithSize在ios7才有,ios6沒有這個方法。
IOS6需要用這個方法,獲得當(dāng)前文字的尺寸,計算內(nèi)部Label的尺寸。
titleW = [self.currentTitle
sizeWithFont:[UIFont
systemFontOfSize:15.0]].width;
4、button 和image在stroyboard的拉伸
注意:
通過storyboard只能拉伸UIImageView,而button在storyboard不能拉伸,只能用代碼實現(xiàn)。
storyboard中 x:表示左側(cè)多少不拉伸 y:表示上邊多少不拉伸 w:表示寬度拉伸多少個像素 h:表示高度拉伸多少個像素 x:0.5(左側(cè)1半不拉伸)
y:0.5(頂部1半不拉伸)
w:0 (寬度拉伸1個像素)
h:0(高度拉伸1個像素)。
//
拉伸按鈕
UIImage
*image = [UIImage
imageNamed:@"NavButton"];
UIImage
*imageH = [UIImage
imageNamed:@"NavButtonPressed"];
image = [image
stretchableImageWithLeftCapWidth:image.size.width
*
0.5
topCapHeight:image.size.height
*
0.5];
imageH = [imageH
stretchableImageWithLeftCapWidth:imageH.size.width
*
0.5
topCapHeight:imageH.size.height
*
0.5];
[_loginBtn
setBackgroundImage:image
forState:UIControlStateNormal];
[_loginBtn
setBackgroundImage:imageH
forState:UIControlStateHighlighted];
5、UICollectionViewController
UICollectionViewController默許有1個UICollectionView,但是self.collectionView
!= self.view
UITableViewController默許有1個UITableView,并且self.tableview
== self.view
UICollectionViewCell是不能通過代碼來創(chuàng)建的,forIndexPath意味著去stroyboard中創(chuàng)建。
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:ID
forIndexPath:indexPath];
UICollectionViewCell
*cell = [[UICollectionViewCell alloc] init];
UICollectionViewCell首先會從緩存池中根據(jù)Identifier查找,如果緩存池中沒有,才會手動創(chuàng)建,但是手動創(chuàng)建init方法沒有提供標(biāo)識符的構(gòu)造方法,在做系統(tǒng)優(yōu)化的時候,就不能根據(jù)Identifier準(zhǔn)確的查找出,會引發(fā)表格的重用。
// 1.注冊cell(告知collectionView將來創(chuàng)建怎樣的cell),利用xib創(chuàng)建
UINib
*nib = [UINib
nibWithNibName:@"SUNProductCell"
bundle:nil];
[self.collectionView
registerNib:nib
forCellWithReuseIdentifier:ID];
// 2.注冊UICollectionViewCell ,如果緩存池中沒有,就會自動創(chuàng)建
[self.collectionView
registerClass:[UICollectionViewCell
class]
forCellWithReuseIdentifier:ID];
注意:在使用UICollectionViewCell 必須傳入布局。
// 1.流水布局
UICollectionViewFlowLayout
*layout = [[UICollectionViewFlowLayout
alloc]
init];
// 2.每一個cell的frame
layout.itemSize
=
CGSizeMake(80,
80);
// 3.設(shè)置cell之間的水平間距
layout.minimumInteritemSpacing
=
0;
// 4.設(shè)置cell之間的垂直間距
layout.minimumLineSpacing
=
10;
// 5.設(shè)置4周的內(nèi)邊距
layout.sectionInset
=
UIEdgeInsetsMake(layout.minimumLineSpacing,
0,
0,
0);
return [super
initWithCollectionViewLayout:layout];
使用UICollectionView
第1步:必須有布局
第2部:cell必須自己注冊
6、解析JSON數(shù)據(jù)
NSString
*fileName = [[NSBundle
mainBundle]
pathForResource:@"products.json"
ofType:nil];
NSData
*data = [NSData
dataWithContentsOfFile:fileName];
NSArray
*jsonArr = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:nil];
7、UIWebView
// 加載資源包中的html
NSURL
*url = [[NSBundle
mainBundle]
URLForResource:_helpItem.html
withExtension:nil];
NSURLRequest
*request = [NSURLRequest
requestWithURL:url];
[webView
loadRequest:request];
//
加載完網(wǎng)頁調(diào)用
- (void)webViewDidFinishLoad:(UIWebView
*)webView
{
NSString *str = [NSString
stringWithFormat:@"window.location.href
= '#%@';",_helpItem.ID];
[webView
stringByEvaluatingJavaScriptFromString:str];
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈