我尝试根据方向更改来更改按钮框架,但按钮操作未检测到框架是否已更改。
“addButtonsOnScrollView
”方法是从“viewWillAppear
”方法调用
这是我的代码
//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);
}
答案 0 :(得分:2)
您是否将按钮添加到滚动视图(从方法名称看起来如此)?旋转时,滚动视图的内容大小很可能无法更新。这就是按钮无法检测触摸的原因。
答案 1 :(得分:1)
最初创建UIButtonTypeRoundRect
而不是UIButtonTypeCustom
。这样您就可以看到Button的可见性。检查按钮可见且可触摸。
另一件事可能是将UIControlEventTouchDown
更改为UIControlEventToucUpInSide
这将有助于我猜测。
答案 2 :(得分:1)
尝试设置containerViewForButtons.clipsToBounds = YES;
只是为了检查按钮是否仍在视图框架中而不是被切断。如果clipsToBounds
设置为NO,您仍会看到按钮,但它不会拦截任何触摸,因为它超出了containerViewForButtons
界限
答案 3 :(得分:1)
试试这个......
- (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];
}