2

我正在尝试根据方向更改更改按钮框架,但按钮操作未检测框架是否更改。

" 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);
}
4

4 回答 4

2

您是否将按钮添加到滚动视图(从方法名称看起来像)?当您旋转时,您的滚动视图的内容大小很可能没有得到更新。这就是按钮无法检测到触摸的原因。

于 2012-06-25T05:52:10.797 回答
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];
}
于 2012-06-25T06:23:54.867 回答
1

最初创建UIButtonTypeRoundRect而不是UIButtonTypeCustom. 这样您就可以看到 Button 的可见性。检查按钮可见且可触摸。

另一件事可能是 Change UIControlEventTouchDownto UIControlEventToucUpInSideThat 会帮助我猜。

于 2012-06-25T05:33:15.530 回答
1

尝试设置containerViewForButtons.clipsToBounds = YES;只是检查按钮是否仍在视图框架中并且没有被切断。如果clipsToBounds设置为 NO,您仍然会看到该按钮,但它不会拦截任何触摸,因为它超出了containerViewForButtons范围

于 2012-06-25T05:37:35.717 回答