2013-12-21 19 views
1

我有一個非常令人困惑的問題。我有一個UIView與幾個按鈕分佈在它。無論什麼原因,當您旋轉視圖(模擬器或設備)時,屏幕右半部分的按鈕變得無法點擊。不可點擊的區域與剛旋轉的方向大小相似。例如,如果您將iPhone設置爲縱向模式並將其設置爲橫向,則不可點擊區域大約是縱向iPhone屏幕的寬度。起初我以爲它們必須是截取點擊的那部分屏幕上的一個不可見元素,但是我將屏幕上所有內容的alpha設置爲1並設置每個元素邊框顏色(通過CALayer),所以我可以檢查。令人驚訝的是,一切都在它應該是的地方,而且它們沒有任何東西(我能找到)覆蓋按鈕。我甚至嘗試經由[self.view bringSubviewToFront:self.BUTTON]屏幕的一半是不可點擊的

使按鈕前I有一個功能,處理設定一切的幀時的視圖是加載或旋轉。這個函數調用是我的旋轉函數和我的viewWillAppear函數中唯一的東西。不可按鈕按鈕問題只發生在設備旋轉時。例如,如果您將設備從縱向旋轉到橫向並且按鈕變得無法點擊,則您只需從底部的選項卡欄中選擇另一個選項卡以加載另一個視圖,然後當您使用查看這個奇怪的問題(沒有旋轉任何東西),按鈕都是可點擊的,並按預期工作。我只是無法理解這個問題的事情是完全相同的代碼被稱爲viewWillAppeardidRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation然而,當設備旋轉和被點擊的按鈕按鈕僅無法點擊。如果用戶單擊另一個選項卡,然後返回帶有此問題的選項卡,則按鈕現在可按預期工作。

更復雜的是,這個問題只發生在ios7。 ios6上的一切都很好。

我已經試過所有我能想到的,我只是沒有一個線索,爲什麼發生這種情況。

下面是一些代碼:

-(void)loadCorrectElements{ 

    if(self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){ 

     //portrait 
     int amountToSubtract = ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)?20:0; 
     int distanceBetweenLabels = (self.view.bounds.size.height - amountToSubtract)/5; 
     self.viewForLabels.frame = CGRectMake(0, distanceBetweenLabels, self.view.bounds.size.width, distanceBetweenLabels*3); 

     int heightOfLabels = (self.viewForLabels.bounds.size.height/3) - 20; 
     int ycoordOfLongitudeLabel = (self.viewForLabels.bounds.size.height/3) + 10; 
     int ycoordOfAltitudeLabel = ((self.viewForLabels.bounds.size.height/3) * 2) + 10; 
     self.latitudeLabel.frame = CGRectMake(10, 10, self.view.bounds.size.width-20, heightOfLabels); 
     self.longitudeLabel.frame = CGRectMake(10, ycoordOfLongitudeLabel, self.view.bounds.size.width-20, heightOfLabels); 
     self.altitudeLabel.frame = CGRectMake(10, ycoordOfAltitudeLabel, self.view.bounds.size.width-20, heightOfLabels); 

     self.optionsButton.frame = CGRectMake(self.view.bounds.size.width-36, self.view.bounds.size.height-36, 26, 26); 

     int ycoordForSpeakButton = self.viewForLabels.frame.origin.y + self.viewForLabels.frame.size.height + 10; 
     self.speakButton.frame = CGRectMake(self.view.bounds.size.width - 40, ycoordForSpeakButton, 30, 25); 

     int heightOfRecordLabel = ((self.view.bounds.size.height - (self.viewForLabels.frame.origin.y + self.viewForLabels.frame.size.height)))/2; 
     int yCoordForRecordButton = self.viewForLabels.frame.origin.y + self.viewForLabels.frame.size.height + (((self.view.bounds.size.height - (self.viewForLabels.frame.origin.y + self.viewForLabels.frame.size.height))/2) - (heightOfRecordLabel/2)); 
     self.recordButton.frame = CGRectMake((self.view.bounds.size.width/2) - (self.view.bounds.size.width/4)/2, yCoordForRecordButton, self.view.bounds.size.width/4, heightOfRecordLabel); 

     int ycoordForOldCoordsButton = self.viewForLabels.frame.origin.y + self.viewForLabels.frame.size.height + (((self.view.bounds.size.height - (self.viewForLabels.frame.origin.y + self.viewForLabels.frame.size.height)))/2 - 12); 
     self.oldCoordsButton.frame = CGRectMake(10, ycoordForOldCoordsButton, 24, 24); 
    }else{ 

     //landscape 
     int amountToSubtract = ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)?20:0; 
     self.viewForLabels.frame = CGRectMake(5, 5 + amountToSubtract, self.view.bounds.size.width-10, self.view.bounds.size.height - 60); 

     int heightOfLabels = (self.viewForLabels.bounds.size.height - amountToSubtract)/3; 
     int ycoordOfLongitudeLabel = (self.viewForLabels.bounds.size.height/3); 
     int ycoordOfAltitudeLabel = ((self.viewForLabels.bounds.size.height/3) * 2); 

     self.latitudeLabel.frame = CGRectMake(5, 0, self.view.bounds.size.width-20, heightOfLabels); 
     self.longitudeLabel.frame = CGRectMake(5, ycoordOfLongitudeLabel, self.view.bounds.size.width-20, heightOfLabels); 
     self.altitudeLabel.frame = CGRectMake(5, ycoordOfAltitudeLabel, self.view.bounds.size.width-20, heightOfLabels); 

     int yCoordForOptionsButton = self.viewForLabels.frame.origin.y + self.viewForLabels.frame.size.height + (((self.view.bounds.size.height - (self.viewForLabels.frame.origin.y + self.viewForLabels.frame.size.height))/2) - 13); 
     self.optionsButton.frame = CGRectMake(self.view.bounds.size.width - 31, yCoordForOptionsButton, 26, 26); 

     self.speakButton.frame = CGRectMake(self.optionsButton.frame.origin.x - 40, self.optionsButton.frame.origin.y, 30, 25); 

     int heightOfRecordLabel = ((self.view.bounds.size.height - (self.viewForLabels.frame.origin.y + self.viewForLabels.frame.size.height)))/2; 
     int yCoordForRecordButton = self.viewForLabels.frame.origin.y + self.viewForLabels.frame.size.height + (((self.view.bounds.size.height - (self.viewForLabels.frame.origin.y + self.viewForLabels.frame.size.height))/2) - (heightOfRecordLabel/2)); 
     self.recordButton.frame = CGRectMake((self.view.bounds.size.width/2) - (self.view.bounds.size.width/4)/2, yCoordForRecordButton, self.view.bounds.size.width/4, heightOfRecordLabel); 
     int ycoordForOldCoordsButton = self.viewForLabels.frame.origin.y + self.viewForLabels.frame.size.height + (((self.view.bounds.size.height - (self.viewForLabels.frame.origin.y + self.viewForLabels.frame.size.height)))/2 - 12); 
     self.oldCoordsButton.frame = CGRectMake(10, ycoordForOldCoordsButton, 24, 24); 

    }//end if 

    [self setLabelText:self.latitudeLabel text:self.latitudeLabel.text]; 
    [self setLabelText:self.longitudeLabel text:self.longitudeLabel.text]; 
    [self setLabelText:self.altitudeLabel text:self.altitudeLabel.text]; 

    self.mainContainer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height); 

}//end method 

-(void)viewWillAppear:(BOOL)animated{ 
    [self loadCorrectElements]; 
}//end function 

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ 
    [self loadCorrectElements]; 
}//end function 

這裏是按鈕聲明裏面viewDidLoad中

//create options button 
     self.optionsButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [self.optionsButton setImage:[UIImage imageNamed:@"Share.png"] forState:UIControlStateNormal]; 
     [self.optionsButton addTarget:self action:@selector(shareButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 
     [self.mainContainer addSubview:self.optionsButton]; 

     //create speak button 
     self.speakButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [self.speakButton setImage:[UIImage imageNamed:@"Sound.png"] forState:UIControlStateNormal]; 
     [self.speakButton addTarget:self action:@selector(speakButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 
     [self.mainContainer addSubview:self.speakButton]; 

     //create record button 
     self.recordButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     self.recordButton.backgroundColor = self.blueColor; 
     [self.recordButton setTitle:NSLocalizedString(@"RECORD", nil) forState:UIControlStateNormal]; 
     self.recordButton.layer.cornerRadius = 3.0f; 
     [self.recordButton addTarget:self action:@selector(recordButtonClicked) forControlEvents:UIControlEventTouchUpInside]; 
     [self.recordButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted]; 
     [self.mainContainer addSubview:self.recordButton]; 

     //set up old coord button 
     self.oldCoordsButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [self.oldCoordsButton setImage:[UIImage imageNamed:@"Clock.png"] forState:UIControlStateNormal]; 
     [self.oldCoordsButton addTarget:self action:@selector(oldCoordsButtonClicked) forControlEvents:UIControlEventTouchUpInside]; 
     [self.mainContainer addSubview:self.oldCoordsButton]; 

回答

3

它看起來像的viewController的視圖或主窗口不調整上旋轉。

正如Jesse Rusak在他的回答中提到的那樣,在您的代碼中進行如此多的計算以設置按鈕來處理旋轉真的沒有意義。

使用AutoResizingMasks。它可以取出數十億行代碼。一旦你掌握了它,你可以移動到自動佈局。

您自動尺寸口罩會有所這樣設置

self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
          UIViewAutoresizingFlexibleHeight | 
          UIViewAutoresizingFixedBottomMargin | 
          UIViewAutoresizingFixedLeftMargin |   
          UIViewAutoresizingFixedRightMargin | 
          UIViewAutoresizingFixedTopMargin; 

同樣,你可以設置按鈕自動尺寸口罩。它會照顧旋轉定位。嘗試一些不同的選項,你會很快得到它的訣竅。

2

這很可能,我認爲你的按鈕的上海華沒有被正確調整。 (瀏覽次數仍然可以將自己的上海華可見外,但他們不會是互動的。)

我會建議使用像RevealSpark Inspector的工具,看看大家的意見後,旋轉等,讓你找出哪一個沒有正確調整大小。另外,您可能希望在將來使用筆尖和自動佈局來佈局佈局,因爲它可以消除所有上述代碼。