当方向改变时以编程方式改变笔尖

发布于 2024-12-14 12:29:57 字数 191 浏览 2 评论 0原文

我在 init 中像这样加载我的笔尖:

self = [super initWithNibName:@"PageView_iPhone" bundle:nil];

但是如果方向发生变化,如何在运行时(即 init 之后)更改它? initWithNibName 将不起作用。

I load my nibs like this in the init:

self = [super initWithNibName:@"PageView_iPhone" bundle:nil];

But how do I change this during runtime (i.e. after the init) if the orientation changes? initWithNibName won't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尴尬癌患者 2024-12-21 12:29:58

如果您确实想加载不同的笔尖,您可以使用:

[[NSBundle mainBundle] loadNibNamed:@"PageView_iPhone" owner:self options:nil];
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"PageView_iPhone"
                                              owner:self
                                            options:nil];

PageView_iPhone* myView = [nibViews objectAtIndex:0];

If you really want to load a different nib, you could use:

[[NSBundle mainBundle] loadNibNamed:@"PageView_iPhone" owner:self options:nil];
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"PageView_iPhone"
                                              owner:self
                                            options:nil];

PageView_iPhone* myView = [nibViews objectAtIndex:0];
弥枳 2024-12-21 12:29:58

在初始化期间,我设置视图(以编程方式创建它们),并为视图控制器中的每个视图的中心位置创建额外的 CGPoint 或为框架创建 CGRect。

在 init 中创建视图后,我调用名为layoutViewsForOrientation 的方法来设置初始位置,并传入当前状态栏方向。当方向改变时,我使用以下代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    BOOL result = NO;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        result = (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        result = YES;
    }
    if( result == YES ) [self layoutViewsForOrientation:interfaceOrientation];
    return result;
}

可能有一种更简单的方法,但这很适合我的需要。

During init, I setup my views (create them programatically) and create extra CGPoint's for center positions or CGRect's for frames for each view in the view controller.

After the views are created in init, I call method named layoutViewsForOrientation to set the initial position, passing in the current status bar orientation. When the orientation changes, I use this code:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    BOOL result = NO;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        result = (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        result = YES;
    }
    if( result == YES ) [self layoutViewsForOrientation:interfaceOrientation];
    return result;
}

There may be an easier way, but this works well for my needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文