[英]adding subView to the UINavigationBar and removing it
我正在尝试创建UINavigationBar的自定义外观。 当用户将应用程序置于横向时,NavigationBar应该覆盖有Image。 当旋转回纵向时,我希望移除View(Image)。 除了将其删除之外,下面的代码都可以正常工作。 如果将代码放在相同的if语句中,则可以删除View,但是如果将代码放在else if语句中,则不会发生任何事情。 我错过了什么吗? / 问候
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
UIView *v = [[UIView alloc]init];
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
v.frame = CGRectMake(0,0,480,44);
v.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"navbar25g.png"]];
[self.view addSubview:v];
//[v removeFromSuperview]; <----- works if I put it here
NSLog(@"LANDSCAPE");//load the landscape view
}
else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
[v removeFromSuperview]; <----- Not working
NSLog(@"PORTRAIT"); //load the portrait view
}
}
无法删除它的原因是每次您进入创建UIView * v新实例的方法时。
创建一个实例变量,然后为其分配视图。 分配后,您可以根据需要删除或添加。
如果您不想使用实例变量,则可以给视图添加标签,即
UIView *v = nil;
v = [self.view viewForTag:1000];
if (!v) {
v = [[UIView alloc] init];
v.tag = 1000;
v.frame = CGRectMake(0, 0, 480, 44);
v.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"navbar25g.png"]];
}
现在,您可以对其进行添加和删除。
您是否尝试过UIAppearance?
[[UINavigationBar appearance] setBackgroundImage:nil barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:image barMetrics:UIBarMetricsLandscapePhone];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.