IOS横屏竖屏问题---2

本文介绍了两种iOS应用中实现横竖屏切换的方法:一种是在代码中为不同屏幕方向加载特定视图;另一种是在同一个XIB文件中包含两个视图,并在旋转时切换显示哪个视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一、横屏竖屏时分别加载两个不同的view,手写view
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        [landscape removeFromSuperview];
        [self.view addSubview:portrait];
    }
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        [portrait removeFromSuperview];
        [self.view addSubview:landscape];
    }
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    
    UIControl *back = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    back.backgroundColor = [UIColor grayColor];
    self.view = back;
    [back release];
    
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    
    portrait = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 440)];
    portrait.backgroundColor = [UIColor yellowColor];
//    [portrait addButton];
    
    
    landscape = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 460, 280)];
    landscape.backgroundColor = [UIColor greenColor];
    [self.view addSubview:portrait];
    
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    NSLog(@"heheh");
    return NO;
    
}
方法二、在一个XIB里面添加两个不同的view,只用改变两个不同view里面控件尺寸,动作事件是一样的
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        [self.landscapeView removeFromSuperview];
        [self.view addSubview:self.portraitView];
    }
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        [self.portraitView removeFromSuperview];
        [self.view addSubview:self.landscapeView];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self.view addSubview:self.portraitView];
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值