界面的搭建

2015-07-28  本文已影响281人  Little_Dragon
- (void)viewDidLoad {
    [super viewDidLoad];
        // 获取按钮的背景图片
    UIImage *image =  _loginBtn.currentBackgroundImage;
    // 将图片进行拉伸。
    image = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
    // 再将修改后的图片赋值给按钮
    [_loginBtn setBackgroundImage:image forState:UIControlStateNormal];
}

// 通常这个方法,用来做cell的动画
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 先将其平移至最右边
    cell.transform = CGAffineTransformMakeTranslation(self.view.width, 0);

    [UIView animateWithDuration:0.5 animations:^{
    // 再将所有形变量删除,返回默认状态
        cell.transform = CGAffineTransformIdentity;
    }];
}


// 同样也可以不使用平移,直接移动它的位置也可以
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.center = CGPointMake(self.view.width, 0);

    [UIView animateWithDuration:0.5 animations:^{
        cell.center = CGPointMake(0, 0);
    }];
}

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

    [self.tableView reloadData];
}
// 布局子控件
- (void)layoutSubviews
{
    [super layoutSubviews];


    if (self.titleLabel.x > self.imageView.x) {
        // 交换两个子控件的位置
        self.titleLabel.x = self.imageView.x;

        NSLog(@"titleLabel-%f",self.titleLabel.x);
    // self.imageView.x = self.titleLabel.width;
        self.imageView.x = CGRectGetMaxX(self.titleLabel.frame);
        [self sizeToFit];
        NSLog(@"imageView-%f",self.imageView.x);

    }

}



// 思想:如果以后系统自带的功能不满足需求,就重写这个方法,添加额外的功能,一定要记得还原之前的功能
- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
    [super setTitle:title forState:state];

    [self sizeToFit];
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
    [super setImage:image forState:state];
    [self sizeToFit];
}

#pragma mark - 自定义tabBar
- (void)setUpTabBar
{


    // 2.添加自己的tabBar
    LXLTabBar *tabBar = [[LXLTabBar alloc] init];

    ...

    tabBar.frame = self.tabBar.bounds;

    [self.tabBar addSubview:tabBar];

}

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

    // 移除系统tabBar上自带的按钮
    for (UIView *childView in self.tabBar.subviews) {
    // 只要不是自定义的tabBar,就给删除
        if (![childView isKindOfClass:[LXLTabBar class]]) {
            [childView removeFromSuperview];
        }

    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 前提条件,覆盖了系统的返回按钮,就不会有滑动返回功能。

    _popDelegate = self.interactivePopGestureRecognizer.delegate;


    self.delegate = self;

}
// 监听什么时候回到跟控制器
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController == self.childViewControllers[0]) { // 跟控制器
        self.interactivePopGestureRecognizer.delegate = _popDelegate;
    }else{
        // 实现导航控制器的滑动返回
        self.interactivePopGestureRecognizer.delegate = nil;
    }
    NSLog(@"%@",viewController);
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // viewController:栈顶控制器,导航条的内容是由栈顶控制器

    // 设置导航条左边按钮,非根控制器才需要
    if (self.childViewControllers.count != 0) { // 非根控制器

        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginRenderingName:@"NavBack"] style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    }

    [super pushViewController:viewController animated:animated];
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}
+ (void)initialize
{
    if(self == [XMGNavigationController class])
    {...
    // 获取当前类下的导航条
    // self->XMGNavigationController
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    // ...
    // 统一设置导航条按钮的颜色
    [bar setTintColor:[UIColor whiteColor]];

    // 获取UIBarButtonItem
    UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedIn:self, nil];

    // 设置导航条返回按钮的文字的位置
    [item setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -64) forBarMetrics:UIBarMetricsDefault];
    }

}

上一篇 下一篇

猜你喜欢

热点阅读