簡體   English   中英

將UIButton Frame設置在UIImgeView的中心

[英]Set UIButton Frame at the center of UIImgeView

這是我在中心設置的代碼

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];

我面臨的問題是,當我改變按鈕的方向時,它的位置會發生變化。 它不會保持在中心位置。 請幫忙

謝謝

其實你應該使用這種方法....

- (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; 
   }

你需要處理這個問題。 你在做什么是正確的。 當方向更改時,您需要進行調整,因為父視圖的尺寸會發生變化。 實現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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM