iOS常用小知识记录-持续更新

iOS常用小知识记录-持续更新_第1张图片
喜欢的类型,哈哈
漂亮的妹子来镇贴~哈哈!话不多说,知识点走起!!!

1.去掉导航栏下面的黑线

方法1(会影响导航栏的translucent透明属性)
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

//视图将要消失时取消隐藏
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}```


######方法2

@property (nonatomic, weak) UIImageView *lineView;

//视图加载完成获取到导航栏最下面的黑线

  • (void)viewDidLoad {
    [super viewDidLoad];

    //获取导航栏下面黑线
    _lineView = [self getLineViewInNavigationBar:self.navigationController.navigationBar];
    }

//视图将要显示时隐藏

  • (void)viewWillAppear:(BOOL)animated
    {
    [super viewWillAppear:animated];

    _lineView.hidden = YES;
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    }

//视图将要消失时取消隐藏

  • (void)viewWillDisappear:(BOOL)animated
    {
    [super viewWillDisappear:animated];

    _lineView.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    }

//找到导航栏最下面黑线视图

  • (UIImageView *)getLineViewInNavigationBar:(UIView *)view
    {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
    return (UIImageView *)view;
    }

    for (UIView *subview in view.subviews) {
    UIImageView *imageView = [self getLineViewInNavigationBar:subview];
    if (imageView) {
    return imageView;
    }
    }

    return nil;
    }

>2.修改navigationBar返回键的颜色

self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];


>3.textField的属性placeholder的字体颜色及大小

self.moneytextfield.placeholder = @"请输入会员消费金额";
[self.moneytextfield setValue:[UIColor lightGrayColor]  forKeyPath:@"_placeholderLabel.textColor"];
[self.moneytextfield setValue:[UIFont systemFontOfSize:16.0] forKeyPath:@"_placeholderLabel.font"];

>4.给Label加中划线或下划线

UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 50, 100, 20)];

NSString  *textStr = @"你好呀";

textLabel.font = [UIFont  systemFontOfSize:15];


//中划线
NSDictionary  *attri = @{NSStrikethroughStyleAttributeName: [NSNumber  numberWithInteger:NSUnderlineStyleSingle]};

// //下划线
// NSDictionary *attri = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};

NSMutableAttributedString  *att = [[NSMutableAttributedString alloc]initWithString:textStr attributes:attri];



textLabel.attributedText = att;

[self.view addSubview:textLabel];

>5 设置navigationItem的字体格式

// 字体大小19,颜色为白色
[nav.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];


>6.解决图片压缩变形的问题

如果是imageView的话直接用  

imageV.contentMode = UIViewContentModeScaleAspectFill;

  imageV.layer.masksToBounds = YES;
button的话我也不知道为啥用着两个属性还是变形,那就把button换成UIimageView加手势,再加上两个属性就行了

>6.自定义返回按钮

UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(backViewcontroller)];

UIBarButtonItem  *fixedItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];


fixedItem.width = -5;

self.navigationItem.leftBarButtonItems = @[fixedItem,leftItem];

你可能感兴趣的:(iOS常用小知识记录-持续更新)