Ayoub Khayati
Ayoub Khayati

Reputation: 121

Buttons displaced/distorted when hiding the navigation bar (After Rotation)

My application has two ViewControllers linked with NavigationController. The First NavigationController has one view with buttons and one view with labels. This view is a circular rotating menu and it represents my home page. The views can rotate with CGAffineTransformMakeRotation - QuartzCore (apparently it's causing the issue) on touchesMoved:.

I wanted to hide the NavigationBar only in this view, so I used setNavigationBarHidden:YES on ViewWillAppear. Then show the bar on ViewWillDisappear.

On the simulator, everything works as expected until i rotate the first ViewController, when i rotate then click on any button (go to the second ViewController) then click on Back (to go back to my first ViewController), everything will be distorted!.

  1. I tried to fix the issue by adding [self.view setBounds:[[UIScreen mainScreen]bounds]]; *[self.view setFrame:[[UIScreen mainScreen]bounds]];* in ViewDidLoad method or ViweWillAppear, the issue remains.
  2. I tried to set NavigationBar alpha to 0 in ViewWillAppear and set it to 1 on ViewWillDisappear and I tried self.navigationController.navigationBar.translucent = YES both options didn't resolve the problem.
  3. I tried to set programmatically views, buttons and labels positions in ViewWillAppear and it doesn't resolve the problem.
  4. I doubted the animation so I removed it but it hasn't any effect on the problem.

As a beginner I'm unable to resolve this issue, please help!

ViewController.h (first ViewController)

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface ViewController : UIViewController

@property (nonatomic, strong) IBOutlet UIView *aView;
@property (nonatomic, strong) IBOutlet UIView *bView;

@property (nonatomic, strong) IBOutlet UIButton *bimg1;
…
@property (strong, nonatomic) IBOutlet UILabel *label1;
…
-(void) rotateTo:(CGFloat)x andY:(CGFloat)y;

@end

ViewController.m

#import "ViewController.h"

@implementation ViewController
@synthesize aView = _aView;
…
@synthesize bimg1 = _bimg1;
…
@synthesize label1 = _label1;
…

static inline double toradians (double degrees) {
return degrees * M_PI/180;
}

-(void)viewDidLoad {
    ![enter image description here][1][super viewDidLoad];
    //Set text font
    [_label1 setFont:[UIFont fontWithName:@"Consolas" size:16]];
…
    [self.view setBounds:[[UIScreen mainScreen]bounds]];
    [self.view setFrame:[[UIScreen mainScreen]bounds]];
}

-(void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
    printf("Aview x: %f     | Aview y: %f \n",self.aView.frame.origin.x, self.aView.frame.origin.y);
    printf("Aview width: %f | Aview height: %f \n",self.aView.frame.size.width,    self.aView.frame.size.height);
}

-(void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint LastTouchPoint = [touch locationInView:self.view];
    CGFloat LasTouchx = LastTouchPoint.x;
    CGFloat LasTouchy = LastTouchPoint.y;

    CGPoint CenterPoint = self.view.center;
    CGFloat x = LasTouchx - CenterPoint.x;
    [self rotateTo:x andY:y];
}

-(void) rotateTo:(CGFloat)x andY:(CGFloat)y {
    CGFloat angle = x/y;
    angle = atan(angle);
    angle = angle * 360/M_PI;
    angle *= 0.0174532925;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:1];
    self.aView.transform=CGAffineTransformMakeRotation(-angle); 
    self.bView.transform=CGAffineTransformMakeRotation(-angle); 

    self.bimg1.transform=CGAffineTransformMakeRotation(angle);
    self.bimg2.transform=CGAffineTransformMakeRotation(angle);
    self.bimg3.transform=CGAffineTransformMakeRotation(angle);
    self.bimg4.transform=CGAffineTransformMakeRotation(angle);

    self.label1.transform=CGAffineTransformMakeRotation(angle);
    self.label2.transform=CGAffineTransformMakeRotation(angle);
    self.label3.transform=CGAffineTransformMakeRotation(angle);
    self.label4.transform=CGAffineTransformMakeRotation(angle);
    [UIView commitAnimations];
}

- (void)viewDidUnload
{
    [self setBimg1:nil];
    … 
    [self setAView:nil];
    [self setBView:nil];
    [super viewDidUnload];
 }

Upvotes: 1

Views: 625

Answers (1)

Ayoub Khayati
Ayoub Khayati

Reputation: 121

Yeah, I figured out the problem… The autoresize of the main View was squeezing my subviews when the NavigationController is hidden or not. so i added self.view.autoresizesSubviews = NO; to ViewDidLoad and the problem is fixed.

Upvotes: 2

Related Questions