在tabBarController模式下怎么对uiviewcontroller xib刷新

Posts - 172,
Articles - 1,
Comments - 84
10:17 by 甘超波, ... 阅读,
// 初始化contentView
[self initContentView];
#pragma mark 初始化contentView
- (void)initContentView {
CGSize size = self.view.bounds.
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height - kTabbarHeight)];
contentView.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
[self.view addSubview:contentView];
[contentView release];
_contentView = contentV
// 设置主题
[self initTheme];
#pragma mark 设置主题
- (void)initTheme {
// 设置导航栏背景
UIImage *navBg = [UIImage resizeImage:@"navigationbar_background.png"];
[[UINavigationBar appearance] setBackgroundImage:navBg forBarMetrics:UIBarMetricsDefault];
// 设置导航栏文字属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
// 设置文字颜色
[attrs setObject:[UIColor blackColor] forKey:UITextAttributeTextColor];
[[UINavigationBar appearance] setTitleTextAttributes:attrs];
// 初始化所有的子控制器
[self initControllers];
#pragma mark 初始化子控制器
- (void)initControllers {
_items = [NSMutableArray array];
HomeController *home = [[HomeController alloc] init];
[self addController:home title:@"首页" normal:@"tabbar_home.png" highlighted:@"tabbar_home_highlighted.png"];
[home release];
MyDataController *data = [[MyDataController alloc] init];
[self addController:data title:@"我的资料" normal:@"tabbar_message_center.png" highlighted:@"tabbar_message_center_highlighted.png"];
[data release];
FriendController *friend = [[FriendController alloc] init];
[self addController:friend title:@"我的关注" normal:@"tabbar_profile.png" highlighted:@"tabbar_profile_highlighted.png"];
[friend release];
FollowerController *follower = [[FollowerController alloc] init];
[self addController:follower title:@"我的粉丝" normal:@"tabbar_discover.png" highlighted:@"tabbar_discover_highlighted.png"];
[follower release];
UIViewController *more = [[UIViewController alloc] init];
more.view.backgroundColor = [UIColor redColor];
[self addController:more title:@"更多" normal:@"tabbar_more.png" highlighted:@"tabbar_more_highlighted.png"];
[more release];
=========================================
#pragma mark 添加子控制器和item
- (void)addController:(UIViewController *)vc title:(NSString *)title normal:(NSString *)normal highlighted:(NSString *)highlighted {
// 初始化item
MJTabbarItemDesc *item = [MJTabbarItemDesc itemWithTitle:title normal:normal highlighted:highlighted];
[_items addObject:item];
// 设置控制器的标题
vc.title =
// 包装控制器
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:vc];
nav1.delegate =
// 对子控制器做了一次retain
[self addChildViewController:nav1];
[nav1 release];
// 初始化tabbar
[self initTabbar];
#pragma mark - 私有方法
#pragma mark 初始化tabbar
- (void)initTabbar {
CGSize size = self.view.bounds.
CGRect frame = CGRectMake(0, size.height - kTabbarHeight, size.width, kTabbarHeight);
MJTabbar *tab = [[MJTabbar alloc] initWithFrame:frame items:_items];
// 设置代理
tab.delegate =
[self.view addSubview:tab];
[tab release];
}============================================
自定义Tabbar
#import &UIKit/UIKit.h&
@protocol MJTabbarDelegate &NSObject&
- (void)tabbarItemChangeFrom:(int)from to:(int)
@interface MJTabbar : UIView
// items : 有多少个标签
- (id)initWithFrame:(CGRect)frame items:(NSArray *)
@property (nonatomic, assign) id&MJTabbarDelegate& delegate;
#import "MJTabbar.h"
#import "MJTabbarItem.h"
@interface MJTabbar() {
// 当前被选中的tabbaritem
MJTabbarItem *_
@implementation MJTabbar
#pragma mark item点击
- (void)itemClick:(MJTabbarItem *)new {
// 设置selected为YES,就能达到UIControlStateSelected状态
if (_current != new) {
if ([self.delegate respondsToSelector:@selector(tabbarItemChangeFrom:to:)]) {
[self.delegate tabbarItemChangeFrom:_current.tag to:new.tag];
_current.userInteractionEnabled = YES;
new.userInteractionEnabled = NO;
new.selected = YES;
_current.selected = NO;
_current = new;
#pragma mark 构造方法
- (id)initWithFrame:(CGRect)frame items:(NSArray *)items {
if (self = [super initWithFrame:frame]) {
// colorWithPatternImage : 平铺一张图片来生成背景颜色
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background.png"]];
int count = items.
CGFloat itemHeight = self.bounds.size.
CGFloat itemWidth = self.bounds.size.width /
for (int index = 0; index & index++) {
MJTabbarItemDesc *desc = [items objectAtIndex:index];
CGFloat itemX = index * itemW
CGRect itemFrame = CGRectMake(itemX, 0, itemWidth, itemHeight);
MJTabbarItem *item = [[MJTabbarItem alloc] initWithFrame:itemFrame itemDesc:desc];
// 设置一个标记
item.tag =
[item addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:item];
[item release];
if (index == 0) {
// 让第0个item选中
[self itemClick:item];
@end自定义导航按钮
#import &UIKit/UIKit.h&
@class MJTabbarItemD
@interface MJTabbarItem : UIButton
- (id)initWithFrame:(CGRect)frame itemDesc:(MJTabbarItemDesc *)
@interface MJTabbarItemDesc : NSObject
@property (nonatomic, copy) NSString * // 标题
@property (nonatomic, copy) NSString * //默认图标
@property (nonatomic, copy) NSString * // 高亮图标
+ (id)itemWithTitle:(NSString *)title normal:(NSString *)normal highlighted:(NSString *)
#import "MJTabbarItem.h"
@implementation MJTabbarItem
- (id)initWithFrame:(CGRect)frame itemDesc:(MJTabbarItemDesc *)desc {
if (self = [super initWithFrame:frame]) {
// 设置高亮显示的背景
[self setHighlightedBg:@"tabbar_slider.png"];
// 设置selected=YES时的背景
[self setSelectedBg:@"tabbar_slider.png"];
// 设置默认的Image
[self setImage:[UIImage imageNamed:desc.normal] forState:UIControlStateNormal];
// 设置selected=YES时的image
[self setImage:[UIImage imageNamed:desc.highlighted] forState:UIControlStateSelected];
// 不需要在用户长按的时候调整图片为灰色
self.adjustsImageWhenHighlighted = NO;
// 设置UIImageView的图片居中
self.imageView.contentMode = UIViewContentModeC
// 设置文字
[self setTitle:desc.title forState:UIControlStateNormal];
// 设置文字居中
self.titleLabel.textAlignment = NSTextAlignmentC
// 设置字体大小
self.titleLabel.font = [UIFont systemFontOfSize:10];
#pragma mark - 覆盖父类的2个方法
#pragma mark 设置按钮标题的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
UIImage *image =
[self imageForState:UIControlStateNormal];
CGFloat titleY = image.size.height - 3;
CGFloat titleHeight = self.bounds.size.height - titleY;
return CGRectMake(0, titleY, self.bounds.size.width,
titleHeight);
#pragma mark 设置按钮图片的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
UIImage *image =
[self imageForState:UIControlStateNormal];
return CGRectMake(0, 0, self.bounds.size.width, image.size.height);
@implementation MJTabbarItemDesc
+ (id)itemWithTitle:(NSString *)title normal:(NSString *)normal highlighted:(NSString *)highlighted {
MJTabbarItemDesc *desc = [[MJTabbarItemDesc alloc] init];
desc.title =
desc.normal =
desc.highlighted =
return [desc autorelease];
- (void)dealloc {
[_title release];
[_normal release];
[_highlighted release];
[super dealloc];
tabbar和导航控制用代理的交互
#pragma mark - MJTabbar的代理方法
// 在这里切换子控制器
- (void)tabbarItemChangeFrom:(int)from to:(int)to {
// 取出旧控制器
UIViewController *old = [self.childViewControllers objectAtIndex:from];
// 移除旧控制器的view
[old.view removeFromSuperview];
// 取出新控制器
UIViewController *new = [self.childViewControllers objectAtIndex:to];
new.view.frame = _contentView.
// 添加新控制器的view
[_contentView addSubview:new.view];
// 执行动画
if (from != to) {
CATransition *anim = [CATransition animation];
anim.duration = 0.15;
anim.type = kCATransitionP
anim.subtype = (to & from)?kCATransitionFromRight:kCATransitionFromL
[_contentView.layer addAnimation:anim forKey:nil];
导航控制器代理方法
#pragma mark 返回
- (void)back {
[_currentController.navigationController popViewControllerAnimated:YES];
#pragma mark home
- (void)home {
[_currentController.navigationController popToRootViewControllerAnimated:YES];
#pragma mark - 导航控制器代理方法
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UIViewController *root = [navigationController.viewControllers objectAtIndex:0];
if (viewController == root) {
// 回到根控制器,显示tabbar
[UIView animateWithDuration:0.3 animations:^{
// 显示tabbar
CGRect frame = _tabbar.
frame.origin.y = self.view.bounds.size.height - _tabbar.bounds.size.
_tabbar.frame =
// 改变contentView的高度
frame = _contentView.
frame.size.height = _tabbar.frame.origin.y;
_contentView.frame =
// 左边的返回按键
UIButton *btn = [[UIButton alloc] init];
UIImage *back = [UIImage imageNamed:@"navigationbar_back.png"];
[btn setBackgroundImage:back forState:UIControlStateNormal];
UIImage *back2 = [UIImage imageNamed:@"navigationbar_back_highlighted.png"];
[btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundImage:back2 forState:UIControlStateHighlighted];
btn.bounds = CGRectMake(0, 0, back.size.width, back.size.height);
UIBarButtonItem *left = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
viewController.navigationItem.leftBarButtonItem =
// 右边的home按键
btn = [[UIButton alloc] init];
UIImage *home = [UIImage imageNamed:@"navigationbar_home.png"];
[btn setBackgroundImage:home forState:UIControlStateNormal];
UIImage *home2 = [UIImage imageNamed:@"navigationbar_home_highlighted.png"];
[btn addTarget:self action:@selector(home) forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundImage:home2 forState:UIControlStateHighlighted];
btn.bounds = CGRectMake(0, 0, home.size.width, home.size.height);
UIBarButtonItem *right = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
viewController.navigationItem.rightBarButtonItem =
// 跳到其他控制器,隐藏tabbar
[UIView animateWithDuration:0.3 animations:^{
// 隐藏tabbar
CGRect frame = _tabbar.
frame.origin.y = self.view.bounds.size.
_tabbar.frame =
// 改变contentView的高度
frame = _contentView.
frame.size.height = self.view.bounds.size.
_contentView.frame =
_currentController = viewCiOS7新特性 ViewController转场切换(一) 以前总结和关键API介绍
@在iOS7之前,View Controller的切换主要有4种:
1. Push/Pop,NavigationViewController
2. Present and dismis Modal
3. UITabBarController
4. addChildViewController(一般用于自定义的继承于 UIViewController 的容器子类)
iOS5,调用- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_(5_0);
(1)前面3种方法这里就不多说了,很常见的方法.至于第四种,我在前面文章-剖析网易标签栏的效果中已经做了阐述,但是它提供的容器转场动画只可以实现一些简单的UIView动画,但是难以重用,耦合高.
(2)关键的API:
A.动画控制器 (Animation Controllers) 遵从 UIViewControllerAnimatedTransitioning 协议,并且负责实际执行动画。
B.交互控制器 (Interaction Controllers) 通过遵从 UIViewControllerInteractiveTransitioning 协议来控制可交互式的转场。
C.转场代理 (Transitioning Delegates) 根据不同的转场类型方便的提供需要的动画控制器和交互控制器。
其中UINavigationControllerDelegate delegate 中新增了2个方法给NavigationController
UIViewControllerTransitioningDelegate 新增transitioningDelegate 给Modal的Present和Dismis
UITabBarControllerDelegate delegate
- (id )tabBarController:(UITabBarController *)tabBarController
interactionControllerForAnimationController: (id )animationController NS_AVAILABLE_IOS(7_0);
- (id )tabBarController:(UITabBarController *)tabBarController
animationControllerForTransitionFromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0);
D.转场上下文 (Transitioning Contexts) 定义了转场时需要的元数据,比如在转场过程中所参与的视图控制器和视图的相关属性。 转场上下文对象遵从 UIViewControllerContextTransitioning 协议,并且这是由系统负责生成和提供的。
E.转场协调器(Transition Coordinators) 可以在运行转场动画时,并行的运行其他动画。 转场协调器遵从 UIViewControllerTransitionCoordinator 协议。
(3)新的API主要提供了2种VC切换的方式:
A.非交互式切换,即定义一种从一个VC到另一个VC的动画效果,切换的时候自动播放,
B.交互式切换,这种方式同样需要定义动画效果,只是这个动画效果会根据跟随交互式手势来切换VC并同时播放动画效果。iOS7提供了一个默认的基于百分比的动画实现 UIPercentDrivenInteractiveTransition,而且根据WWDC的说明,最简单的实现交互式动画的方法就是通过继承 UIPercentDrivenInteractiveTransition。
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'在tabBarController模式下怎么对UIViewController刷新_百度知道
在tabBarController模式下怎么对UIViewController刷新
在该控制类里面写一个方法:- (void)setDataSource{
tabview.dataSource =
tabview.dataSource =}在AppDelegate.m中调用:- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if(viewController.tabBarItem.tag == 3){
UINavigationController *navigationctr = (UINavigationController *)viewC
SecondViewController *secvc = (SecondViewController *)navigationctr.topViewC
[secvc setDataSource];
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁在tabBarController模式下怎么对UIViewController刷新_百度知道
在tabBarController模式下怎么对UIViewController刷新
提问者采纳
(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if(viewController.tabBarItem.dataSource =
[secvc setDataSource];
SecondViewController *secvc = (SecondViewController *)navigationctr.m中调用;
tabview:- (void)setDataSource{}在AppDelegate.tag == 3){
UINavigationController *navigationctr = (UINavigationController *)viewController:- (void)tabBarController.topViewController在该控制类里面写一个方法.dataSource = self
电子产品技术支持
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 uiviewcontroller横屏 的文章

 

随机推荐