[英]Button action not working when button frame is changed
我正在尝试根据方向更改来更改按钮框架,但按钮操作未检测到框架是否已更改。
从“ viewWillAppear
”方法调用“ addButtonsOnScrollView
”方法
这是我的代码
//scaleFactor = 320 if portrait orientation scaleFactor= 480 if landscape orientation
- (void) addButtonsOnScrollView
{
containerViewForButtons = [[UIView alloc]initWithFrame:CGRectMake(scaleFactor-100, 22, 100, 37)];
addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton addTarget:self action:@selector(addFriend:)forControlEvents:UIControlEventTouchDown];
[addButton setBackgroundImage:[UIImage imageNamed:@"add_btn.png"] forState:UIControlStateNormal];
addButton.frame = CGRectMake(0, 0, 37, 37);
[containerViewForButtons addSubview:addButton];
[self.view addSubview:containerViewForButtons];
}
- (void) addFriend:(UIButton *) button {
NSLog("Button clicked....");
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[self layoutLandscapeView];
}
else if (interfaceOrientation == UIInterfaceOrientationPortrait) {
[self layoutPortraitView];
}
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void) layoutLandscapeView {
containerViewForButtons.frame = CGRectMake(380, 22, 100, 37);
}
- (void) layoutPortraitView {
containerViewForButtons.frame = CGRectMake(220, 22, 100, 37);
}
您是否正在将按钮添加到滚动视图(从方法名称看起来像它)? 旋转时,很有可能滚动视图的内容大小没有得到更新。 这就是为什么按钮无法检测到触摸的原因。
最初创建UIButtonTypeRoundRect
而不是UIButtonTypeCustom
。 这样您就可以看到Button的可见性。 选中按钮可见且可触摸。
另一件事可能更改UIControlEventTouchDown
到UIControlEventToucUpInSide
这将有助于我猜。
尝试设置containerViewForButtons.clipsToBounds = YES;
只是要检查按钮是否仍在视图框架中并且未被切断。 如果clipsToBounds
设置为NO,您仍然会看到该按钮,但是它不会拦截任何触摸,因为它超出了containerViewForButtons
范围
尝试这个......
- (void) layoutLandscapeView
{
[containerViewForButtons removeFromSuperview];
containerViewForButtons.frame = CGRectMake(380, 22, 100, 37);
[self.view addSubview:containerViewForButtons];
}
- (void) layoutPortraitView
{
[containerViewForButtons removeFromSuperview];
containerViewForButtons.frame = CGRectMake(220, 22, 100, 37);
[self.view addSubview:containerViewForButtons];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.