【问题标题】:How to add UITabbarController to UIViewController in iOs如何在 iOs 中将 UITabbarController 添加到 UIViewController
【发布时间】:2013-08-11 03:57:43
【问题描述】:

如何用 TabbarController 推送 Viewcontroller?在 Viewcontroller'XIB 中,我创建了 UITabbarController。然后我推送这个 ViewController,但它没有出现 UITabbarController。 这是我的代码: *Viewcontroller.h:

 @interface StatusViewController : UIViewController<UITabBarControllerDelegate>
{
    IBOutlet UITabBarController *tabBarController;

    IBOutlet UIButton *UploadButton;
    IBOutlet UIButton *ConvertorButton;
    IBOutlet UIButton *CompletedButton;
}
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end

*Viewcontroller.m:

@implementation StatusViewController
@synthesize tabBarController ;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createTabView];
}
-(void)createTabView
{
    ....

    ....
    [ConvertorButton addSubview:Label3];

    [CompletedButton setTag:3];


    [CompletedButton setImage:[UIImage imageNamed:@"Overlay-2.png"] forState:UIControlStateNormal];
    [CompletedButton setImage:[UIImage imageNamed:@"background.jpg"] forState:UIControlStateSelected];
    [CompletedButton setImage:[UIImage imageNamed:@"background.jpg"] forState:UIControlStateHighlighted];

    //[[CompletedButton layer] setBorderWidth:2.0f];
    // [[CompletedButton layer] setBorderColor:[UIColor grayColor].CGColor];

    UILabel *Label4=[[UILabel alloc]initWithFrame:CGRectMake(6, 8, 70, 30)];
    [Label4 setTextAlignment:UITextAlignmentCenter];
    [Label4 setText:@"Completed"];


    Label4.backgroundColor=[UIColor clearColor];
    [Label4 setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
    Label4.textColor=[UIColor whiteColor];
    [CompletedButton addSubview:Label4];

    // [self selectTab:1];

    [self.view addSubview:UploadButton];

    [self.view addSubview:ConvertorButton];

    [self.view addSubview:CompletedButton];


}

感谢您的帮助

【问题讨论】:

  • 你在这里提出的问题是不可理解的..请让它正确吗?
  • 你动态改变你的rootview,你可以做到。推送UITabbarcontoller需要什么

标签: iphone ios objective-c uiviewcontroller uitabbarcontroller


【解决方案1】:

如果您想在 UIViewController 类中使用 UITabBarController,请使用以下代码...

UIViewController .h 类 -

    @property (nonatomic, retain) UITabBarController *tab;

UIViewController .m 类 -

在 ViewDidLoad 方法中添加这个...

    self.tab=[[UITabBarController alloc]init];

    // FirstViewController
    First *fvc=[[First alloc]initWithNibName:nil bundle:nil];
    fvc.title=@"First";
    fvc.tabBarItem.image=[UIImage imageNamed:@"i.png"];

    //SecondViewController
    Second *svc=[[Second alloc]initWithNibName:nil bundle:nil];
    svc.title=@"Second";
    svc.tabBarItem.image=[UIImage imageNamed:@"im.png"];

    //ThirdViewController
    Third *tvc=[[Third alloc]initWithNibName:nil bundle:nil];
    tvc.title=@"Third";
    tvc.tabBarItem.image=[UIImage imageNamed:@"img.png"];

    self.tab.viewControllers=[NSArray arrayWithObjects:fvc, svc, tvc, nil];

    [self.view addSubview:self.tab.view];

这里的 First、Second 和 Third 是三个不同的 UIViewController。而且您不需要在选项卡上执行操作。

它会起作用的......

【讨论】:

  • 谢谢,我试过你的代码,它可以工作,但 tabbaritem 不在底部位置。不知道为什么?
  • 好的..您是否正在为普通屏幕 320 * 480 创建项目....它不会显示 320 * 568 屏幕。你应该根据你的屏幕尺寸设置框架坐标..
  • 我看过你的截图,你肯定用的是 iphone (Retina 4.0 inch) 模拟器..
  • self.tab=[[UITabBarController alloc]init]; CGRect screenBounds = [[UIScreen mainScreen] bounds]; if (screenBounds.size.height == 568) { // 4 英寸屏幕的代码 // LoginButton.frame = CGRectMake(0, 518, 80, 49); self.tab.view.frame = CGRectMake(0,0,320,568); } else if (screenBounds.size.height == 1024) { //ipad 代码 } else { // 3.5 英寸屏幕代码 // LoginButton.frame = CGRectMake(0, 430, 80, 49); self.tab.view.frame = CGRectMake(0,0,320,480); } 但它不正确
  • 是的,这完全错了伙计..你想在屏幕底部或顶部的标签栏..????
【解决方案2】:

如果您是初学者,最佳做法是在 google 中搜索最新的 api 示例,然后了解代码并创造自己的世界。

1) 相关示例代码可以在苹果here找到。

2) TweetieBar -- 这是带有自定义 TabBarController(TweetieTabBar) 的示例代码

【讨论】:

    猜你喜欢
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多