簡體   English   中英

在TabBarViewController中布置視圖

[英]Laying out views in a TabBarViewController

我正在嘗試手動(以編程方式)在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 ,超級視圖的框架是窗口的整個大小,因此按鈕被選項卡欄隱藏了。 我究竟做錯了什么?

在沒有看到更多代碼的情況下,您似乎對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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM