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系统会自动帮我们自动加上八个小时

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值