gpt4 book ai didi

ios - 当调用状态在 ios objective c 中结束时无法更改 UIView 的 rect

转载 作者:行者123 更新时间:2023-11-29 02:36:39 27 4
gpt4 key购买 nike

当调用状态将状态栏高度增加到 40 时,我需要调整 View 。我使用 - (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame 使用 NSNotificationCenter

它在状态栏高度从 20 增加到 40 时起作用,但是当状态栏高度从 40 减少到 20 时 NSNotificationCenter 似乎没有发布通知,因为 UIView 框架没有分配给所需的 CGRectMake

我正在做一个旧项目,所以没有 Storyboard。

这是我尝试过的

在viewDidLoad中——

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarChangedForBottmView:) name:@"STATUSBARCHANGED" object:self];

和方法

- (void) statusBarChangedForBottmView:(NSNotification *)notification
{
NSDictionary *data = [notification userInfo];
NSString *statusBar = [data valueForKey:@"frame"];
NSLog(@"status bar in edit card = %@",statusBar);
if ([statusBar isEqualToString:@"STATUS"])
{
if (STATUSBARINCREASED)
{
[self.bottomView setFrame:CGRectMake(0, self.view.frame.size.height-60, self.view.frame.size.width, 45)];
}
else
{
[self.bottomView setFrame:CGRectMake(0, self.view.frame.size.height-40, self.view.frame.size.width, 45)];
}
}

}

最佳答案

编辑:

确保您订阅了来自任何 对象的通知。即:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarChangedForBottmView:) name:@"STATUSBARCHANGED" object:nil]; // <-- object:nil

这不是发布通知的当前对象 (self),而是您的 AppDelegate。

来自文档:

notificationSender

The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer. If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.

如果你得到这个工作,我仍然建议使用 UIApplicationWillChangeStatusBarFrameNotification,而不是从 - (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect) 发布你自己的通知newStatusBarFrame.


我刚刚在一个新项目上进行了测试,使用 UIApplicationWillChangeStatusBarFrameNotification 效果非常好。

- (void)viewDidLoad {
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarChanged:) name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
}

- (void)statusBarChanged:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
NSValue *statusBarRectValue = userInfo[UIApplicationStatusBarFrameUserInfoKey];
CGRect statusBarRect = [statusBarRectValue CGRectValue];
NSLog(@"%@", NSStringFromCGRect(statusBarRect));
}

输出:

接听电话:

2014-10-09 17:53:31.407 test-so[14775:8742949] {
UIApplicationStatusBarFrameUserInfoKey = "NSRect: {{0, 0}, {375, 40}}";
}

通话结束:

2014-10-09 17:53:35.881 test-so[14775:8742949] {
UIApplicationStatusBarFrameUserInfoKey = "NSRect: {{0, 0}, {375, 20}}";
}

关于ios - 当调用状态在 ios objective c 中结束时无法更改 UIView 的 rect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26282600/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com