我有一个更好的解决方案。只需做一件事,在您的 .h 文件中实现这一行
@interface yourViewController:UIViewController
{
UIButton *buttonWineries;
}
并将此代码实现到 .m 文件中
-(void)ViewDidLoad
{
[super viewDidLoad];
buttonWineries = [[UIButton alloc]init];
buttonWineries.layer.borderColor = [UIColor lightGrayColor].CGColor;
buttonWineries.layer.borderWidth = 1.0;
buttonWineries.layer.cornerRadius = 5;
[buttonWineries setBackgroundColor:[UIColor darkGrayColor]];
[self.wineriesMenu addSubview:buttonWineries];
yPositionWineries += 190;
[buttonWineries addTarget:self action:@selector(btnWineryDetails:) forControlEvents:UIControlEventTouchDown];
BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);
// now do whatever you need
if(isPortrait)
[buttonWineriessetFrame:CGRectMake(10,yPositionWineries, 300, 160)];
else
[buttonWineriessetFrame:CGRectMake(//your frame size for lanscape)];
}
// check your orientation angel and change your button size inside this delegate method
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
// Return YES for supported orientations
if (willRotateToInterfaceOrientation == UIInterfaceOrientationPortrait) {
if (buttonWineries)
[buttonWineries setFrame:CGRectMake(//put your frame size here)];
}
if (willRotateToInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
if (buttonWineries)
[buttonWineries setFrame:CGRectMake(//put your frame size here)];
}
}
或者
您可以添加 didRotateFromInterfaceOrientation 代替 willRotateToInterfaceOrientation 以在调整大小或位置更改时停止动画
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// Return YES for supported orientations
if (willRotateToInterfaceOrientation == UIInterfaceOrientationPortrait) {
if (buttonWineries)
[buttonWineries setFrame:CGRectMake(//put your frame size here)];
}
if (willRotateToInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
if (buttonWineries)
[buttonWineries setFrame:CGRectMake(//put your frame size here)];
}
if (fromInterfaceOrientation == UIDeviceOrientationLandscapeRight) {
if (buttonWineries)
[buttonWineries setFrame:CGRectMake(//put your frame size here)];
}
}
我希望它对你有帮助。如果您仍然遇到任何问题,请告诉我。谢谢