我有一個非常令人困惑的問題。我有一個UIView
與幾個按鈕分佈在它。無論什麼原因,當您旋轉視圖(模擬器或設備)時,屏幕右半部分的按鈕變得無法點擊。不可點擊的區域與剛旋轉的方向大小相似。例如,如果您將iPhone設置爲縱向模式並將其設置爲橫向,則不可點擊區域大約是縱向iPhone屏幕的寬度。起初我以爲它們必須是截取點擊的那部分屏幕上的一個不可見元素,但是我將屏幕上所有內容的alpha設置爲1並設置每個元素邊框顏色(通過CALayer
),所以我可以檢查。令人驚訝的是,一切都在它應該是的地方,而且它們沒有任何東西(我能找到)覆蓋按鈕。我甚至嘗試經由[self.view bringSubviewToFront:self.BUTTON]
屏幕的一半是不可點擊的
使按鈕前I有一個功能,處理設定一切的幀時的視圖是加載或旋轉。這個函數調用是我的旋轉函數和我的viewWillAppear
函數中唯一的東西。不可按鈕按鈕問題只發生在設備旋轉時。例如,如果您將設備從縱向旋轉到橫向並且按鈕變得無法點擊,則您只需從底部的選項卡欄中選擇另一個選項卡以加載另一個視圖,然後當您使用查看這個奇怪的問題(沒有旋轉任何東西),按鈕都是可點擊的,並按預期工作。我只是無法理解這個問題的事情是完全相同的代碼被稱爲viewWillAppear
和didRotateFromInterfaceOrientation:(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];