2

向きの変更に応じてボタンのフレームを変更しようとしていますが、フレームが変更されたかどうかをボタンの操作で検出できません。

" addButtonsOnScrollView"methodeは" 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

最初UIButtonTypeRoundRectにの代わりに作成しますUIButtonTypeCustom。ボタンの可視性を確認できるようにします。チェックボタンが表示され、タッチ可能です。

もう1つは、ChangetoThatが推測に役立つかもしれませUIControlEventTouchDownUIControlEventToucUpInSide

于 2012-06-25T05:33:15.530 に答える
1

containerViewForButtons.clipsToBounds = YES;ボタンがまだビューフレームにあり、切り取られていないかどうかを確認するためだけに設定してみてください。NOに設定されている場合でもボタンは表示されますが、範囲外clipsToBoundsであるため、タッチはインターセプトされません。containerViewForButtons

于 2012-06-25T05:37:35.717 に答える
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 に答える