iOS項目筆記-----記賬項目中的小問題

遇到的問題

1.navigationBar隱藏後,tableView的第一個cell與頂部有間隙

//這是ViewController中的代碼,
//隱藏導航欄
    self.navigationController.navigationBarHidden = YES;
//tableView的第一個cell與頂部有間隙
//解決:
    self.automaticallyAdjustsScrollViewInsets = NO;

self.automaticallyAdjustsScrollViewInsets 是什麼意思

2. 給視圖設置一層漸變的Layer


 //設置一層漸變的灰色效果,
    CAGradientLayer *grayLayer = [CAGradientLayer layer];
    //設置過程中的顏色
    grayLayer.colors = @[((__bridge id)[UIColor whiteColor].CGColor),
                         ((__bridge id)[UIColor blackColor].CGColor)];
    //填充開始的點,與結束的點 (0-1,0-1)
    grayLayer.startPoint = CGPointMake(0, 0);
    grayLayer.endPoint = CGPointMake(0, 1);
    grayLayer.frame = backgroundImage.bounds;
    grayLayer.opacity = 0.3;
    [backgroundImage.layer addSublayer:grayLayer];

3.設置textfield的placeholder字體顏色

 //設置placeholder字體顏色
    _searchText.placeholder = @"搜索目的地/餐廳/菜品/食記/用戶";
    NSDictionary *attrabutes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
    NSAttributedString *placegolderAttribute = [[NSAttributedString alloc] initWithString:_searchText.placeholder attributes:attrabutes];
    [_searchText setAttributedPlaceholder:placegolderAttribute];

4.狀態欄問題

狀態欄的字體爲黑色:UIStatusBarStyleDefault
狀態欄的字體爲白色:UIStatusBarStyleLightContent

APP啓動頁狀態欄顏色設置
在info.plist添加 Status bar style,改變style值,就可以改變顏色,默認是Gray style

一、在info.plist中,將View controller-based status bar appearance設置爲NO,白色,YES,黑色
如果View controller-based status bar appearance爲YES。
則[UIApplication sharedApplication].statusBarStyle 無效。

解決個別VC中狀態欄字體顏色不同的辦法
二、在app delegate中:

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

三、在個別狀態欄字體顏色不一樣的vc中

-(void)viewWillAppear:(BOOL)animated
{
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

用下面的方法:
1、在vc中重寫vc的preferredStatusBarStyle方法。
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleDefault;
}
2、在viewDidload中調用:[self setNeedsStatusBarAppearanceUpdate];

但是,當vc在nav中時,上面方法沒用,vc中的preferredStatusBarStyle方法根本不用被調用。
原因是,[self setNeedsStatusBarAppearanceUpdate]發出後,
只會調用navigation controller中的preferredStatusBarStyle方法,
vc中的preferredStatusBarStyley方法跟本不會被調用。

解決辦法有兩個:
方法一:
設置navbar的barStyle 屬性會影響status bar 的字體和背景色。如下。
//status bar的字體爲白色
//導航欄的背景色是黑色。
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

//status bar的字體爲黑色
//導航欄的背景色是白色,狀態欄的背景色也是白色。
//self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

方法二:
自定義一個nav bar的子類,在這個子類中重寫preferredStatusBarStyle方法:

MyNav* nav = [[MyNav alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;

@implementation MyNav

- (UIStatusBarStyle)preferredStatusBarStyle
{
UIViewController* topVC = self.topViewController;
return [topVC preferredStatusBarStyle];
}

5.創建懸浮按鈕

#pragma mark - 添加懸浮按鈕
- (void)viewWillAppear:(BOOL)animated {

    //創建按鈕
    _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _addButton.frame = CGRectMake(0, 0, 66, 66);
    [_addButton addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_addButton setImage:[UIImage imageNamed:@"topicContent_add"] forState:UIControlStateNormal];

    //創建UIWindow
    _addWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 66, 66)];
    _addWindow.center = CGPointMake(kScreenWidth / 2, kScreenHeight - (49 + 20 + 33));
    _addWindow.windowLevel = UIWindowLevelAlert + 1;
    [_addWindow addSubview:_addButton];
    [_addWindow makeKeyAndVisible];
}

- (void)viewWillDisappear:(BOOL)animated {

    if (_addWindow == nil) {
        return;
    }
    [_addWindow resignKeyWindow];
    _addWindow = nil;

}

6.關於NSDate,獲取年月日

(1)NSDateFormat獲取
NSDate *date =[NSDate date];//簡書 FlyElephant
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

[formatter setDateFormat:@"yyyy"];
NSInteger currentYear=[[formatter stringFromDate:date] integerValue];
[formatter setDateFormat:@"MM"];
NSInteger currentMonth=[[formatter stringFromDate:date]integerValue];
[formatter setDateFormat:@"dd"];
NSInteger currentDay=[[formatter stringFromDate:date] integerValue];

NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",date,currentYear,currentMonth,currentDay);

(2)NSDateComponents獲取

NSDate  *currentDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];

NSInteger year=[components year];
NSInteger month=[components month];
NSInteger day=[components day];
NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",currentDate,year,month,day);

以上兩種方式都能輕鬆獲取年月日,iOS默認的NSDate是格林尼治時間,比中國時區的時間少8個小時,處理過日期的都知道8個小時的誤差存在,我們在獲取年月日的時候不要加上8個小時,iOS系統會自動幫我們自動加上八個小時

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章