Ios 我想添加一个将在整个应用程序中保持的视图?

Ios 我想添加一个将在整个应用程序中保持的视图?,ios,iphone,uiviewcontroller,Ios,Iphone,Uiviewcontroller,我想添加一个将在整个应用程序中保持的视图? 我怎样才能做到这一点 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 430, 320, 50)]; myView.backgroundColor = [[UIColor redColor]colorWithA

我想添加一个将在整个应用程序中保持的视图? 我怎样才能做到这一点

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 430, 320, 50)];
    myView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
    [self.window addSubview:myView];
这是行不通的。
提前感谢…

实现这一点有几种方法。不清楚是希望每个视图控制器中都有这样的视图,还是希望每个视图控制器中都有相同的实例

由于我看不出有任何理由拥有一个共享实例(根据您的描述),我的方法是将
UIViewController
子类化,我们称之为
SOMyViewController
,然后从
SOMyViewController
继承应用程序中的所有视图控制器

然后,我将覆盖
SOMyViewController
的“viewDidLoad”方法,如下所示:

- (void) viewDidLoad {
    [super viewDidLoad];

    if ([self addMyCustomView]) {
        UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 430, 320, 50)];
        myView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
        [self.window addSubview:myView];    
    }
}

/**
 Override this in all your subclasses to decide whether to display the custom view or not
 */
- (BOOL) addMyCustomView {
    return YES;
}
static UIView *mySharedView;

+ (void) initialize {
    mySharedView = [[UIView alloc]initWithFrame:CGRectMake(0, 430, 320, 50)];
    mySharedView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
}

- (void) viewDidLoad {
    [super viewDidLoad];

    if ([self addMyCustomView]) {
        [self.window addSubview:mySharedView];    
    }
}

/**
 Override this in all your subclasses to decide whether to display the custom view or not
 */
- (BOOL) addMyCustomView {
    return YES;
}
如果您希望在视图控制器之间共享同一个实例,我将更改上述代码,如下所示:

- (void) viewDidLoad {
    [super viewDidLoad];

    if ([self addMyCustomView]) {
        UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 430, 320, 50)];
        myView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
        [self.window addSubview:myView];    
    }
}

/**
 Override this in all your subclasses to decide whether to display the custom view or not
 */
- (BOOL) addMyCustomView {
    return YES;
}
static UIView *mySharedView;

+ (void) initialize {
    mySharedView = [[UIView alloc]initWithFrame:CGRectMake(0, 430, 320, 50)];
    mySharedView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
}

- (void) viewDidLoad {
    [super viewDidLoad];

    if ([self addMyCustomView]) {
        [self.window addSubview:mySharedView];    
    }
}

/**
 Override this in all your subclasses to decide whether to display the custom view or not
 */
- (BOOL) addMyCustomView {
    return YES;
}

您需要带有视图的
UIViewController
子类,将其设置为
rootViewController
,它将被持久化您的上述代码将正常工作,但您应该避免像y=430这样的硬编码。因为iPhone4和iPhone5的高度不同。另外,如果您想在当前视图控制器上查看“myView”。您应该将当前视图控制器的视图颜色设置为“清晰颜色”。我希望根据某些条件在所有屏幕上使用它。我想知道如何使用它,方法是在appdelegate中声明视图,然后从其他类中使用它…如果您指的是拥有一个应用程序范围的实例,您所要做的就是从
SOMyViewController
继承视图控制器。如果使用第二块代码,您会看到
UIView
声明为静态,并在
initialize
方法中初始化一次。没有必要使用appdelegateYeah你是对的,我也是这样做的,我在窗口中添加了视图。我需要进一步的帮助,我的意思是我想用一些动画来显示创建的视图…我认为你必须尝试动画,如果你遇到任何问题或坚持做某件特定的事情,请在这里发布另一个问题。我认为动画超出了这个问题的范围。有很多教程,raywenderlich.com可能是一个很好的起点