在TabBarViewController中布局视图

时间:2013-11-25 15:12:52

标签: ios uitabbarcontroller

我正在尝试手动(以编程方式)在UITabBarViewController中布局视图。我像这样实例化我的UITabBarViewController:

MYAppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UITabBarController *tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];

    MYViewController1 *myViewController1 = [[MYViewController1 alloc] init];
    myViewController1.title = @"My VC 1";
    [tabBarController addChildViewController:myViewController1];

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.rootViewController = tabBarController;

    return YES;
}

MYViewController1.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    _myView = [[MYView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:_myView];
}

MYView.m

- (void)layoutSubviews
{
    CGRect descriptionRect, buttonRect;
    CGRectDivide(self.frame, &buttonRect, &descriptionRect, 50.f, CGRectMaxYEdge);

    _descriptionTextView.frame = descriptionRect;
    [self addSubview:_descriptionTextView];

    _myButton.frame = buttonRect;
    [self addSubview:_myButton];
}

我遇到的问题是当我到达layoutSubviews时,superview的框架是窗口的完整大小,因此按钮被标签栏隐藏。我做错了什么?

1 个答案:

答案 0 :(得分:0)

如果没有看到更多代码,您可能会对使用UITabBarController感到困惑。

在上面发布的代码中,您正在初始化UITabBarController,然后是UIViewController,然后在tabBarController上调用'addChildViewController:'。 (但是,addChildViewController:是UIViewController上的一个实例方法)。

这是否尝试将ViewController添加为TabBarController的选项卡?如果是这样,那么尝试使用以下代码代替addChildViewController:看它是否为您提供了您正在寻找的功能:

tabBarController.viewControllers = @[myViewController1]; // Passing an array of viewControllers will set them as a tabs on the TabBar in the order they are added to the array.

如果这没有帮助,那么请对此答案发表评论,并提供有关代码所需功能的更多详细信息,我会尽快更新答案,以便为您提供帮助。

编辑:看起来我可能误解了你的问题。您可以在子视图的初始化中尝试以下代码:

_myView = [[MYView alloc] initWithFrame:self.view.bounds]; // Try bounds instead of frame.
相关问题