UITabBarController - 初始框架和调整大小

发布于 2024-10-20 13:10:53 字数 1528 浏览 1 评论 0原文

我发现了这个: 子类 UITabBarController 来调整其框架 它解释了如何实现什么我的目标是在答案中。为了解释那里的帖子,您必须注册才能在 TabBarController 的选定视图更改时收到通知,然后在那里应用您的几何图形。否则它会被 TabBarController 震撼。


我正在尝试调整 UITabBarController 的大小,以在某些任务发生时显示 20px 高的状态消息。奇怪的是,如果我单步执行调整 TabBarController 大小的方法,框架的原点就会改变。

当视图首次出现时,UITabBarController 的 y 原点设置为 0。即使 applicatioinScreen y.origin 为 20。如果我在应用程序首次加载时调整大小,一切都会关闭。子视图不会调整大小。第一次调整大小后,如果我查看不同的选项卡,TabBarController 会将自身调整为正确的大小,并且对调整大小方法的后续调用确认了这一点。

这就是我安装 UITabBarcontroller 的方式:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    [tabBarController setViewControllers:[AppController getGroupViewControllerArray] animated:NO];
    [window addSubview:tabBarController.view]; 
    [window makeKeyAndVisible]; 
    return YES;
}

这就是我调整 TabBarController 大小的方式:

-(void)resizeTabToShowActivity {

    CGRect newFrame = mainTabBarController.view.frame;
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    newFrame.origin.y = applicationFrame.origin.y + 20;
    newFrame.size.height = applicationFrame.size.height - 20;
    mainTabBarController.view.frame = newFrame;

}

我注意到,如果我在窗口中安装 TabBarController 后直接调整大小,则一切正常。出于调试目的,我也将调整大小方法移至 AppDelegate 中,但仍然没有任何乐趣。

谢谢

I found this: Subclass UITabBarController to adjust its frame which explains how to achieve what I was aiming for in the answer. To paraphrase the post there, you have to register to be notified when the TabBarController's selected view changes, and then apply your geometry there. Otherwise it gets blown away by the TabBarController.


I am attempting to resize my UITabBarController to show a 20px high status message when certain tasks are ocurring. The curious thing is that if I step through the method which resizes the TabBarController, the origin of the frame changes.

When the view first appears, the UITabBarController's y origin is set to 0. Even though the applicatioinScreen y.origin is 20. If I resize when the app first loads, everything is off. The subviews do not resize. After this first resize, if I view a different tab, the TabBarController adjusts itself to the proper size, and subsequent calls to my resizing method confirm this.

This is how I am installing the UITabBarcontroller:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    [tabBarController setViewControllers:[AppController getGroupViewControllerArray] animated:NO];
    [window addSubview:tabBarController.view]; 
    [window makeKeyAndVisible]; 
    return YES;
}

And this is how I am resizing the TabBarController:

-(void)resizeTabToShowActivity {

    CGRect newFrame = mainTabBarController.view.frame;
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    newFrame.origin.y = applicationFrame.origin.y + 20;
    newFrame.size.height = applicationFrame.size.height - 20;
    mainTabBarController.view.frame = newFrame;

}

I have noticed that if I resize directly after installing the TabBarController in the window, everything works fine. For debugging purposes, I've moved the resize method into the AppDelegate as well, and still no joy.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

無心 2024-10-27 13:10:53

我能够通过将 UITabBarController 包含在父 UIViewController 中并将父 UIViewController 作为应用程序的根视图控制器来调整其大小。

这是我的父 UIViewController

// .h file
@interface MyTabBarViewController : UIViewController <UITabBarControllerDelegate>
@property(nonatomic, strong) IBOutlet UITabBarController *tbc;
@end

// .m file
@implementation MyTabBarViewController

@synthesize tbc=_tbc;

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    if (_tbc == nil) {
        CGRect frame = [[UIScreen mainScreen] bounds];
        // arbitrary numbers, just to illustrate the point
        CGRect smallFrame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width-300, frame.size.height-100);

        _tbc = [[UITabBarController alloc] init];
        _tbc.viewControllers = @[[[MyTabBarViewController1 alloc] init],   [[MyTabBarViewController2 alloc] init]];
        //_tbc.view.backgroundColor = [UIColor blueColor];
        _tbc.view.autoresizesSubviews = YES;
        _tbc.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _tbc.view.frame = smallFrame;

        [self.view addSubview:_tbc.view];
    }
}

MyViewController1 和 MyViewController2 只是具有 UILabel 的通用 UIViewController 子类。

这是我的应用程序委托代码,用于加载父 UIViewController

@implementation MyTabBarAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    MyTabBarViewController *vc = [[MyTabBarViewController alloc] init];
    vc.view.backgroundColor = [UIColor yellowColor];
    vc.definesPresentationContext = NO;

    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
    return YES;
}

I was able to resize a UITabBarController by containing it within a parent UIViewController, and making the parent UIViewController as the root view controller for the app.

Here's my parent UIViewController

// .h file
@interface MyTabBarViewController : UIViewController <UITabBarControllerDelegate>
@property(nonatomic, strong) IBOutlet UITabBarController *tbc;
@end

// .m file
@implementation MyTabBarViewController

@synthesize tbc=_tbc;

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    if (_tbc == nil) {
        CGRect frame = [[UIScreen mainScreen] bounds];
        // arbitrary numbers, just to illustrate the point
        CGRect smallFrame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width-300, frame.size.height-100);

        _tbc = [[UITabBarController alloc] init];
        _tbc.viewControllers = @[[[MyTabBarViewController1 alloc] init],   [[MyTabBarViewController2 alloc] init]];
        //_tbc.view.backgroundColor = [UIColor blueColor];
        _tbc.view.autoresizesSubviews = YES;
        _tbc.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _tbc.view.frame = smallFrame;

        [self.view addSubview:_tbc.view];
    }
}

MyViewController1 and MyViewController2 are just generic UIViewController subclasses that have a UILabel.

Here's my app delegate code that loads the parent UIViewController

@implementation MyTabBarAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    MyTabBarViewController *vc = [[MyTabBarViewController alloc] init];
    vc.view.backgroundColor = [UIColor yellowColor];
    vc.definesPresentationContext = NO;

    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
    return YES;
}
浅黛梨妆こ 2024-10-27 13:10:53

UITabBarController 通常从 wantsFullScreenLayout 返回 YES,这意味着当用作根视图控制器时,其视图的大小将适合全屏,包括状态栏下方的区域。然后,控制器根据需要调整其子视图的大小,以避开状态栏下方的区域。

您可以尝试将 wantsFullScreenLayout 设置为 NO,这应该使其大小不会像您预期的那样位于状态栏下方。

The UITabBarController normally returns YES from wantsFullScreenLayout, which means that its view when used as the root view controller will be sized for the full screen including the area under the status bar. The controller then sizes its subviews to avoid the area under the status bar, as needed.

You can try setting wantsFullScreenLayout to NO, which should make it be sized to not be under the status bar as you seem to be expecting.

热风软妹 2024-10-27 13:10:53

在 UITabBarController 子类上尝试一下:

- (void)viewDidLoad {
    [super viewDidLoad];

    // A UITabBarController's view has two subviews: the UITabBar and a container UITransitionView that is
    // used to hold the child views. Save a reference to the container.
    for (UIView *view in self.view.subviews) {
        if (![view isKindOfClass:[UITabBar class]]) {
            [view setFrame:RESIZED_FRAME];

        }
    }
}

Try this on UITabBarController subclass:

- (void)viewDidLoad {
    [super viewDidLoad];

    // A UITabBarController's view has two subviews: the UITabBar and a container UITransitionView that is
    // used to hold the child views. Save a reference to the container.
    for (UIView *view in self.view.subviews) {
        if (![view isKindOfClass:[UITabBar class]]) {
            [view setFrame:RESIZED_FRAME];

        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文