我正在创建一个自定义 UIView 以充当带有工具栏的“弹出”窗口。工具栏中每个 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);
}
长按标记为链接的按钮会导致视图在其上方弹出。看到弹出视图按钮显示正常,但单击它们会导致它们后面的按钮动作被触发(好像它忽略了视图)。
注意:我还尝试为 UIBarButtonItem 创建一个带有 UIButton 的自定义视图,但它没有任何区别。
更新:这里是超级视图的边界和包含相关按钮的视图:超级视图边界:0.000000,0.000000,768.000000,1024.000000 子视图边界:0.000000,912.000000,224.000000,56.000000 所以子视图在超级视图的约束范围内