美文网首页
ios 笔记

ios 笔记

作者: 想聽丿伱說衹愛我 | 来源:发表于2016-06-17 13:53 被阅读65次

1. Label的自动高度自适应与属性字符串

UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor greenColor];
label.textColor = [UIColor whiteColor];
label.numberOfLines = 0;
NSString *str = @"123451dsa;flmdkalsjflnm\nf,.a\nsdl;fjsajldkfjlkasdf23455";
NSMutableAttributedString *noteStr =[[NSMutableAttributedString alloc] initWithString:str];
NSRange redRange = NSMakeRange(0, 20);
[noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
[label setAttributedText:noteStr];
[label sizeToFit];
NSDictionary *attributes = @{NSFontAttributeName : label.font};
CGSize size =[str boundingRectWithSize:CGSizeMake(100, 0) options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
label.frame = CGRectMake(100, 100, size.width, size.height);
[self.view addSubview:label];

2. cell label高度自适应

  • 首先label的numofline设置为0
  • 其次在cell的m文件中
    (void)cellForHeight:(NSArray *)itemData
    { 
    _content.lineBreakMode = NSLineBreakByWordWrapping;
    _content.preferredMaxLayoutWidth = CGRectGetWidth(_content.frame);
    _content.text = itemData[2];
    }
  • 随意一个方法中将label的lineBreakMode和lineBreakMode设置好并赋好label.text的值

  • 最后实现tableview的delegate的方法

    (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    BWMFoundCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BWMFoundCell"];
    if (!cell){
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BWMFoundCell" owner:self options:nil];
    for (id oneObj in nib){
    if ([oneObj isKindOfClass:[BWMFoundCell class]]){
    cell = (BWMFoundCell *)oneObj;
    }}}
    NSArray *item = _dataArr[indexPath.row];
    [cell cellForHeight:item];
    CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize];
    return size.height +1;
    }

3. 自定义navigationbar

  • 通过重新navigationcontroler的initwithrootviewcontroller来实现
    (instancetype)initWithRootViewController:(BWMFoundDetail *)rootViewController{
    self = [super initWithRootViewController:rootViewController];
    if (self)
    {
    [self.view insertSubview:[uiview new] belowSubview:self.navigationBar];
    [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsCompact];
    self.navigationBar.layer.masksToBounds = YES;}
    return self;
    }
// [uiview new]即为替换navigationbar的视图
  • 通过改变backgroundimage和setShadowImage
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                                  forBarMetrics:UIBarMetricsDefault];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setShadowImage:nil];
    [self.navigationController.navigationBar setBackgroundImage:nil
                                                  forBarMetrics:UIBarMetricsDefault];
}

4. 更改navigationbar左右按钮位置

UIButton *buttonItem = [UIButton buttonWithType:UIButtonTypeCustom];
buttonItem.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);//
[buttonItem setImage:[UIImage imageNamed:@"icon_arrow_back"] forState:UIControlStateNormal];
[buttonItem sizeToFit];
buttonItem.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[buttonItem addTarget:self
               action:@selector(backAction)
     forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItem];
self.navigationItem.leftBarButtonItem = leftBarItem;
  • buttonItem.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0) 当为图片时使用
  • buttonItem.titleEdgeInsets 为字符串时使用

5. 全局设ui appdelegate中

//设置UI
[[UINavigationBar appearance] setBarTintColor:BWMColor(253, 127, 3, 1)];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setTintColor:BWMColor(247, 104, 0, 1)];
[[UINavigationBar appearance] setTitleTextAttributes:@{
    NSForegroundColorAttributeName : [UIColor whiteColor],
    NSFontAttributeName : [UIFont boldSystemFontOfSize:16]
}];

6. 去除左按钮箭头旁边的字 并可自定义

  • 新建一个类别 内容如下面所示
    #import "UINavigationItem+CustomBackButtom.h"
    #import <objc/runtime.h>

    @implementation UINavigationItem (CustomBackButtom)

    + (void)load
    {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
      Method originalMethodImp = class_getInstanceMethod(self, @selector(backBarButtonItem));
      Method destMethodImp =
          class_getInstanceMethod(self, @selector(myCustomBackButton_backBarbuttonItem));
      method_exchangeImplementations(originalMethodImp, destMethodImp);
    });
    }

    - (UIBarButtonItem *)backBarButtonItem
    {
    return [[UIBarButtonItem alloc] initWithTitle:@""
                                            style:UIBarButtonItemStyleBordered
                                           target:nil
                                           action:nil];
    }

    static char kCustomBackButtonKey;
    - (UIBarButtonItem *)myCustomBackButton_backBarbuttonItem
    {
    UIBarButtonItem *item = [self myCustomBackButton_backBarbuttonItem];
    if (item)
    {
        return item;
    }
    item = objc_getAssociatedObject(self, &kCustomBackButtonKey);
    if (!item)
    {
        item = [[UIBarButtonItem alloc] initWithTitle:@""
                                                style:UIBarButtonItemStyleBordered
                                               target:nil
                                               action:nil];
        objc_setAssociatedObject(self, &kCustomBackButtonKey, item,
                                 OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return item;
    }

    @end

7. 防止键盘挡住输入框

  • 在Podfile中添入 pod 'IQKeyboardManager', '~> 3.3.7'
  • //设置键盘事件
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
    manager.shouldResignOnTouchOutside = YES;
    manager.shouldToolbarUsesTextFieldTintColor = YES;
    manager.enableAutoToolbar = NO;

8. 通过xib读取uivew

  • 在init初始化方法中

      NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"BWMDetailXYJ" owner:self options:nil];
      UIView *view = [nib lastObject];
      self = (uiview *)view;
    

9. 获得当前view的viewcontroller

    - (UIViewController *)rootViewController {
        for (UIView* next = [self superview]; next; next = next.superview) {
            UIResponder *nextResponder = [next nextResponder];
            if ([nextResponder isKindOfClass:[UIViewController class]]) {
                return (UIViewController *)nextResponder;
            }
        }
        return nil;
    }

10. 自定义UITableViewCell(registerNib: 与 registerClass: 的区别)

11. macdown语法

12. 不定参数的使用

    + (void)test:(NSString *)string, ...   
    {  
        va_list args;  
        va_start(args, string);  
        if (string)   
        {  
            NSString *otherString;  
            while ((otherString = va_arg(args, NSString *)))   
            {  
                //依次取得所有参数  
            }  
        }  
        va_end(args);  
    }  
  • 说明:

      va_list args:
      
      //定义一个指向个数可变的参数列表指针;
      
      va_start(args,string)://string为第一个参数,也就是最右边的已知参数,这里就是获取第一个可选参数的地址.使参数列表指针指向函数参数列表中的第一个可选参数,函数参数列表中参数在内存中的顺序与函数声明时的顺序是一致的。
      
      va_arg(args,NSString):返回参数列表中指针所指的参数,返回类型为NSString,并使参数指针指向参数列表中下一个参数。  
      
      a_end(args):清空参数列表,并置参数指针args无效
    

13. 隐藏Tabbar

BWMAllDevices *viewController = [BWMAllDevices new];
        viewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:viewController animated:YES];
self.hidesBottomBarWhenPushed = YES;
BWMAllDevices *viewController = [BWMAllDevices new];
        [self.navigationController pushViewController:viewController animated:YES];
self.hidesBottomBarWhenPushed = NO;

13. 关于(0,0)的探究

  • 当有navigationbar时,tableview、scrollview的(0,0)在navigationbar下面的左上角。然而其他的view的0点却是在屏幕的左上角。是什么原因引起的呢。
  • 从ios7以后,控制器有了一个新的属性,automaticallyAdjustsScrollViewInsets, 默认是YES,如果视图里面存在唯一一个UIScorllView或其子类view,那么,它会自动设置相应的内边距,这样可以让scorll占据整个视图,又不会让导航栏遮盖。
  • 所以,要将scrollview及子view的0点变为屏幕左上角
self.automaticallyAdjustsScrollViewInsets = NO;
或者是
 self.scrollView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);

14.

相关文章

网友评论

      本文标题:ios 笔记

      本文链接:https://www.haomeiwen.com/subject/rldvdttx.html