屏幕的一半是不可点击的

时间:2013-12-21 02:16:23

标签: ios iphone cocoa-touch uiview ios7

我有一个非常令人困惑的问题。我有一个UIView,上面有几个按钮。无论出于何种原因,当您旋转视图(模拟器或设备)时,屏幕右半部分的按钮将变得不可点击。不可点击的区域类似于刚刚旋转的方向的大小。例如,如果你有一个处于纵向模式的iPhone并将其转为横向,则不可点击的区域大约是纵向的iPhone屏幕宽度。起初我认为它们必须是屏幕上拦截点击的那一部分上的一个不可见元素,但随后我将屏幕上所有内容的alpha设置为1并设置每个元素的边框颜色(通过CALayer所以我可以检查一下。令人惊讶的是,一切都应该是它应该是的地方,并且它们没有(我能找到)覆盖按钮。我甚至尝试通过[self.view bringSubviewToFront:self.BUTTON]

将按钮带到前面

我有一个函数可以在加载或旋转视图时处理设置所有内容的帧。这个函数调用是我的旋转函数和我的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];

2 个答案:

答案 0 :(得分:3)

看起来viewController的视图或mainWindow没有在旋转时调整大小。

正如Jesse Rusak在他的回答中提到的那样,在代码中进行如此多的计算来设置按钮来处理旋转是没有意义的。

使用AutoResizingMasks。它可以取出数十亿行代码。掌握它后,您可以转到自动布局。

您的自动调整遮罩可以像这样设置

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

同样,您可以为按钮设置自动调整遮罩。并且它将负责旋转的定位。尝试一些不同的选择,你很快就会掌握它。

答案 1 :(得分:2)

我觉得你的按钮的超级视图没有正确调整大小。 (视图仍然可以在超级视图之外看到,但它们不会是交互式的。)

我建议您使用RevealSpark Inspector之类的工具来查看轮播后的观看位置,这样就可以确定哪一个未正确调整大小。

另外,您可能希望将来使用nib和自动布局为布局,因为它将消除上述所有代码。