iOS UIBarButtonItem 动作落到它后面的按钮上
iOS UIBarButtonItem action falls through to button behind it
我正在创建一个自定义 UIView 来充当带有工具栏的 "pop up" window。工具栏中每个 UIBarButtonItem 的操作在 iPhone 模拟器上按预期触发,但在 iPad 或 iPad 模拟器上未触发。相反,触发弹出视图后面的按钮的操作。我的目标是 iOS 8. 可能是什么问题?
这是创建自定义视图的地方。这是在 UIView 的子类中。它包含选择器:
-(void)setupButtons: (SandBoxViewController*)ctrl :(UIImage*)img1 :(UIImage*)img2 :(UIImage*)img3 :(CGRect) rect
{
controller=ctrl;
CGRect frame = CGRectMake(rect.origin.x-12, rect.origin.y, rect.size.width*4, TOOLBARH);
toolbar = [[UIToolbar alloc]initWithFrame:frame];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *customItem1 = [[UIBarButtonItem alloc]
initWithImage:img1 style:UIBarButtonItemStylePlain
target:self action:@selector(setToOne:)];
UIBarButtonItem *customItem2 = [[UIBarButtonItem alloc]
initWithImage:img2 style:UIBarButtonItemStylePlain
target:self action:@selector(setToTwo:)];
UIBarButtonItem *customItem3 = [[UIBarButtonItem alloc]
initWithImage:img3 style:UIBarButtonItemStylePlain
target:self action:@selector(setToThree:)];
NSMutableArray *toolbarItems = [NSMutableArray arrayWithObjects:customItem1, customItem2,customItem3,nil];
[toolbar setItems:toolbarItems];
[self addSubview:toolbar];
}
这将创建自定义视图并调用上述函数。它位于 ViewController:
-(void)setupLongPressButtonView: (CGRect)frame
{
self.buttonView =[[LinkButtonView alloc]initWithFrame:frame];
UIImage *image1=[[UIImage imageNamed:@"connect1.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *image2=[[UIImage imageNamed:@"connect2.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *image3=[[UIImage imageNamed:@"connect3.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//retrieves the button that I want the popup to be placed above
UIBarButtonItem *button=[self.toolbarItems objectAtIndex:self.linkButtonIndex];
CGRect buttonFrame=button.customView.frame;
[self.buttonView setupButtons:self :image1 :image2 :image3 :buttonFrame];
[self.view addSubview:self.buttonView];
CGRect superv=self.view.frame;
CGRect subv=self.buttonView.frame;
NSLog(@"superview bounds: %f,%f,%f,%f. subview bounds: %f,%f,%f,%f",superv.origin.x,superv.origin.y,superv.size.width,superv.size.height,subv.origin.x,subv.origin.y,subv.size.width,subv.size.height);
}
单击并长按标记为 Link 的按钮会导致在其上方弹出视图。看到弹出视图按钮显示正常,但单击它们会触发按钮后面的操作(就好像它忽略了视图一样)。
注意:我还尝试为 UIBarButtonItem 创建一个带有 UIButton 的自定义视图,但没有任何区别。
更新:这里是超级视图的边界和包含相关按钮的视图:
监督范围:0.000000,0.000000,768.000000,1024.000000
子视图范围:0.000000,912.000000,224.000000,56.000000
所以 sub 在 super
的约束范围内
as if its ignoring the view
完全正确。它是忽略它。弹出视图在其父视图的范围之外。默认情况下,在其父视图范围之外的视图是不可触及的。点击它就好像它不存在一样。这是完全正常的预期行为。
我正在创建一个自定义 UIView 来充当带有工具栏的 "pop up" window。工具栏中每个 UIBarButtonItem 的操作在 iPhone 模拟器上按预期触发,但在 iPad 或 iPad 模拟器上未触发。相反,触发弹出视图后面的按钮的操作。我的目标是 iOS 8. 可能是什么问题?
这是创建自定义视图的地方。这是在 UIView 的子类中。它包含选择器:
-(void)setupButtons: (SandBoxViewController*)ctrl :(UIImage*)img1 :(UIImage*)img2 :(UIImage*)img3 :(CGRect) rect
{
controller=ctrl;
CGRect frame = CGRectMake(rect.origin.x-12, rect.origin.y, rect.size.width*4, TOOLBARH);
toolbar = [[UIToolbar alloc]initWithFrame:frame];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *customItem1 = [[UIBarButtonItem alloc]
initWithImage:img1 style:UIBarButtonItemStylePlain
target:self action:@selector(setToOne:)];
UIBarButtonItem *customItem2 = [[UIBarButtonItem alloc]
initWithImage:img2 style:UIBarButtonItemStylePlain
target:self action:@selector(setToTwo:)];
UIBarButtonItem *customItem3 = [[UIBarButtonItem alloc]
initWithImage:img3 style:UIBarButtonItemStylePlain
target:self action:@selector(setToThree:)];
NSMutableArray *toolbarItems = [NSMutableArray arrayWithObjects:customItem1, customItem2,customItem3,nil];
[toolbar setItems:toolbarItems];
[self addSubview:toolbar];
}
这将创建自定义视图并调用上述函数。它位于 ViewController:
-(void)setupLongPressButtonView: (CGRect)frame
{
self.buttonView =[[LinkButtonView alloc]initWithFrame:frame];
UIImage *image1=[[UIImage imageNamed:@"connect1.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *image2=[[UIImage imageNamed:@"connect2.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *image3=[[UIImage imageNamed:@"connect3.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//retrieves the button that I want the popup to be placed above
UIBarButtonItem *button=[self.toolbarItems objectAtIndex:self.linkButtonIndex];
CGRect buttonFrame=button.customView.frame;
[self.buttonView setupButtons:self :image1 :image2 :image3 :buttonFrame];
[self.view addSubview:self.buttonView];
CGRect superv=self.view.frame;
CGRect subv=self.buttonView.frame;
NSLog(@"superview bounds: %f,%f,%f,%f. subview bounds: %f,%f,%f,%f",superv.origin.x,superv.origin.y,superv.size.width,superv.size.height,subv.origin.x,subv.origin.y,subv.size.width,subv.size.height);
}
单击并长按标记为 Link 的按钮会导致在其上方弹出视图。看到弹出视图按钮显示正常,但单击它们会触发按钮后面的操作(就好像它忽略了视图一样)。
注意:我还尝试为 UIBarButtonItem 创建一个带有 UIButton 的自定义视图,但没有任何区别。
更新:这里是超级视图的边界和包含相关按钮的视图: 监督范围:0.000000,0.000000,768.000000,1024.000000 子视图范围:0.000000,912.000000,224.000000,56.000000 所以 sub 在 super
的约束范围内as if its ignoring the view
完全正确。它是忽略它。弹出视图在其父视图的范围之外。默认情况下,在其父视图范围之外的视图是不可触及的。点击它就好像它不存在一样。这是完全正常的预期行为。