私はそれに対するより良い解決策を持っています。.h ファイルにこの行を実装することを 1 つ実行するだけです
@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)];
}
}
また
サイズ変更または位置の変更中にアニメーションを停止するには、 willRotateToInterfaceOrientation の代わりに didRotateFromInterfaceOrientation を追加できます
- (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)];
}
}
お役に立てば幸いです。問題が解決しない場合はお知らせください。ありがとう