简体   繁体   中英

Rotate UIButton on change of orientation

I have round button which consists of the following image

Now on rotation of device I want to rotate the button in a way that the image maintains the orientation.

How can I achieve this,

I do not want to change the frame of the button i want it to stay where it is I just want to transform the button.

If you have an IBOutlet Object.

theButton.transform = CGAffineTransformMakeRotation(M_PI / -4);

If you want to create run time button on view.

- (void)viewDidLoad {
  [super viewDidLoad];
  UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
  [btn setFrame:CGRectMake(100, 100, 200, 44)];
  btn.transform=CGAffineTransformMakeRotation(M_PI / -4);
  [btn setTitle:@"RakeshBhatt" forState:UIControlStateNormal];
  [self.view addSubview:btn];
}

To rotate a image view try this

// Set the anchor point and center so the view swings from the upper right
imageView.layer.anchorPoint = CGPointMake(1.0, 0.0);
imageView.center = CGPointMake(CGRectGetWidth(self.view.bounds), 0.0);

// Rotate 90 degrees to hide it off screen
CGAffineTransform rotationTransform = CGAffineTransformIdentity;
rotationTransform = CGAffineTransformRotate(rotationTransform, DegreesToRadians(90));
swingView.transform = rotationTransform;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM