gpt4 book ai didi

ios - 无法点击 4"iPhone 左侧的按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:02:49 24 4
gpt4 key购买 nike

这绝对令人困惑。在 3.5"iPhone 模拟器上,我的应用程序上的所有 UIButtons 都工作正常。但是,当我在 4"iPhone 模拟器上启动时,应用程序左侧的所有 UIButtons 都没有收到任何点击事件。

以下是 3.5"尺寸和 4"尺寸的屏幕截图。在 4"尺寸上,我添加了一条线。在那条线的左侧,没有任何按钮收到点击事件。在那条线的右侧,所有按钮都正常运行。按钮 2、5 和 8 的左侧执行不响应点击,但这些按钮的右侧确实响应。

enter image description here

enter image description here

更新----感谢@iccir,我发现了更多信息。显然,我的 UIWindow 只有 320x480 而不是它应该的 568x320。除了使它成为关键和可见之外,我没有在我的代码中触及 UIWindow。在我的 MainWindow.xib 中,我将它的 IBOutlet 连接到我的 rootViewController。

<UIWindow: 0xc097d50; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM; gestureRecognizers = <NSArray: 0xc098460>; layer = <UIWindowLayer: 0xc097e70>>

我惊呆了。知道为什么 UIWindow 的大小不正确吗?

最佳答案

这是一个很常见的问题:您的 UIButton 超出了其父 View 之一的范围。如果 clipsToBounds/masksToBounds 设置为 NO(默认值),您的 UIButton 仍会显示,但不会向其发送触摸事件。

让我们简化这个案例。假设一个具有以下代码的 View Controller :

- (void) viewDidLoad
{
[super viewDidLoad];

UIColor *fadedRedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.25];
UIColor *fadedBlueColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.25];

CGRect containerFrame = CGRectMake(25, 25, 100, 100);
CGRect buttonFrame = CGRectMake(100, 100, 64, 44);

UIView *container = [[UIView alloc] initWithFrame:containerFrame];

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"Button" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setFrame:buttonFrame];
[button addTarget:self action:@selector(_handleButton:) forControlEvents:UIControlEventTouchUpInside];

[container setBackgroundColor:fadedRedColor];
[button setBackgroundColor:fadedBlueColor];

[container addSubview:button];

[[self view] addSubview:container];
}

- (void) _handleButton:(id)sender
{
NSLog(@"Moooooo!");
}

看起来像这样: Screenshot 1

按钮包含在 container 中,但位于容器边界之外(容器宽 100 像素,高 100 像素,按钮的原点在 100, 100).

当您触摸屏幕时,UIKit 将从 View 层次结构 (UIWindow) 的顶部开始并递归调用 -[UIView hitTest:withEvent:] 直到它找到应该处理的 View 触摸。然而,在这个例子中,UIKit 永远不会下降到容器中(因为你触摸到了它的边界之外),因此按钮 subview 不会被点击。

如果我们改为将 buttonFrame 更改为 50, 50,它看起来像这样: Screenshot 2

按钮与容器重叠的部分会响应触摸事件。位于容器外部的部分不会:

Screenshot 3

要调试不是完全可触摸的 View ,您可以尝试如下调试功能:

static void sDebugViewThatIsntTouchable(UIView *view)
{
UIView *superview = [view superview];

while (superview) {
CGRect rectInSuperview = [view convertRect:[view bounds] toView:superview];

CGPoint topLeft = CGPointMake(CGRectGetMinX(rectInSuperview), CGRectGetMinY(rectInSuperview));
CGPoint topRight = CGPointMake(CGRectGetMaxX(rectInSuperview), CGRectGetMinY(rectInSuperview));
CGPoint bottomLeft = CGPointMake(CGRectGetMinX(rectInSuperview), CGRectGetMaxY(rectInSuperview));
CGPoint bottomRight = CGPointMake(CGRectGetMaxX(rectInSuperview), CGRectGetMaxY(rectInSuperview));

if (![superview pointInside:topLeft withEvent:nil]) {
NSLog(@"Top left point of view %@ not inside superview %@", view, superview);
}

if (![superview pointInside:topRight withEvent:nil]) {
NSLog(@"Top right point of view %@ not inside superview %@", view, superview);
}

if (![superview pointInside:bottomLeft withEvent:nil]) {
NSLog(@"Bottom left point of view %@ not inside superview %@", view, superview);
}

if (![superview pointInside:bottomRight withEvent:nil]) {
NSLog(@"Bottom right point of view %@ not inside superview %@", view, superview);
}

superview = [superview superview];
}
};

编辑:正如您在评论中提到的,罪魁祸首 View 是主 UIWindow,其大小为 320x480 而不是 320x568。 在 xib 中打开“启动时全屏”解决了这个问题

当然,问题是:“为什么?” :)

如果您在文本编辑器中打开 xib 文件,您会注意到窗口的宽度为 320,高度为 480。当 xib 在启动时被解码时,窗口最初是用这个 320x480 帧构建的。

然后 UIKit 查询 -[UIWindow resizesToFullScreen](一个私有(private)方法)。如果返回 YES,则 UIWindow 执行与 [self setFrame:[[self window] bounds]] 等效的操作。

在 Interface Builder 中切换“启动时全屏”标志会直接切换私有(private) UIWindow.resizesToFullScreen 标志。

关于ios - 无法点击 4"iPhone 左侧的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18996161/

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