将UIButton Frame设置在UIImgeView的中心

时间:2011-11-28 11:25:55

标签: iphone ios

以下是我在中心设置的代码

UIButton *videoImage = [[UIButton alloc]initWithFrame:CGRectMake((secondImageView.frame.size.width/2)-50,(secondImageView.frame.size.height/2)-50,50,50)];
videoImage.transform = CGAffineTransformMakeRotation(ang*(3.14/180));
[videoImage setBackgroundImage:[UIImage imageNamed:@"PlayButton.png"] forState:UIControlStateNormal];
[videoImage addTarget:self action:@selector(PlayMusicOnClickofButton:) forControlEvents:UIControlEventTouchUpInside];
[secondImageView addSubview:videoImage];
videoImage.tag  = k+1000;
secondImageView.userInteractionEnabled = YES;
[videoImage release];

我面临的问题是当我改变按钮的方向时,它的位置会发生变化。它不会保持在中心位置。请帮忙

由于

2 个答案:

答案 0 :(得分:3)

实际上你应该使用这种方法......

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
   {
         if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
         {
                    //handle how you want to show in landscape mode
         }
         else
         {
                    //handle how you want to show in portrait mode
         }
         return YES; 
   }

答案 1 :(得分:1)

你需要处理这个问题。你在做什么是正确的。当方向更改时,您需要进行调整,因为父视图的尺寸会发生变化。实施willAnimateRotationToInterfaceOrientation委托。看看这个 -

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                         duration:(NSTimeInterval)duration
{
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        //handle how you want to show in landscape mode
    }
    else
    {
        //handle how you want to show in portrait mode
    }
}