【问题标题】:How to add the view to whole screen如何将视图添加到整个屏幕
【发布时间】:2019-02-15 09:22:58
【问题描述】:

我有一个 2 vc,从一个屏幕推送到另一个屏幕。在我的 vc2 中,我以编程方式添加导航栏标题,栏按钮。但是当我添加视图时,从顶部减少了大约 10 个点。没有完全显示。我尝试了所有的可能性。还附上图片:

在我的 vc2 中:

- (void)viewDidLoad {
    [super viewDidLoad];
    _menuView.hidden = YES;
    [self navItems];
}

    -(void)navItems{

        UIImage* filterImage = [UIImage imageNamed:@"filter.png"];
        CGRect frameimg = CGRectMake(0,0, 15,15);

        filterBtn = [[UIButton alloc] initWithFrame:frameimg];
        [filterBtn setBackgroundImage:filterImage forState:UIControlStateNormal];

        [filterBtn addTarget:self action:@selector(MenuTap:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *filter =[[UIBarButtonItem alloc] initWithCustomView:filterBtn];
        self.navigationItem.rightBarButtonItem = filter;

        self.title = @"Demo";
         self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    }
- (IBAction)MenuTap:(id)sender
{


     _menuView.hidden = NO;

//1
//   [self.navigationController.view addSubview:_menuView];
//    [self.view addSubview:_menuView];

    //2
//    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
//    [window addSubview: _menuView];
//

//3
   // UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
    //[currentWindow addSubview:_menuView];


//4
  //[[UIApplication sharedApplication].windows.lastObject addSubview:_menuView];

//5
   // self.navigationController.navigationBar.layer.zPosition = -1;
    //[self.view addSubview:_menuView];

}

但是不能完全展示任何想法吗?

【问题讨论】:

  • 你的问题不清楚。你提到的导航栏,橙色视图
  • 橙色是我的看法。顶部是导航栏的正常视图。我需要展示我的橙色视图来咬。这样顶部的白色也不应该显示
  • 添加框架_menuView.frame = [[UIScreen mainScreen] bounds]
  • 使用哪个代码。我尝试了所有的可能性。我需要在哪一行添加这个?
  • 是否可以附加您的项目

标签: ios objective-c xcode view navbar


【解决方案1】:

根据您在此处添加的详细答案,如果您想显示在​​状态栏下方,那么您首先需要获取状态栏高度,然后您需要添加您需要的 y 位置。

 - (IBAction)MenuTap:(id)sender
{
  _menuView.hidden = NO;
  //initially getStatusbar height
  float statusBarHeight = [self statusBarHeight];
  // assign your subview to window bounds
  _menuView.frame = [[UIScreen mainScreen] bounds];
  CGRect frameRect = _menuView.frame;
  // convert your y- coordinates based on your need
  frameRect.origin.y = statusBarHeight;
 _menuView.frame = frameRect;
 [self.navigationController.view addSubview:_menuView];

}



-(float) statusBarHeight
{
    CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
    return MIN(statusBarSize.width, statusBarSize.height);
}

【讨论】:

【解决方案2】:

我正在使用@Anbu.Karthic 解决方案进行更新。

在您的(IBAction)MenuTap:(id)sender 中使用以下代码更新代码。它会起作用的。我试过了。

- (IBAction)MenuTap:(id)sender
{
  _menuView.hidden = NO;
 [self.navigationController.view addSubview:_menuView];
  _menuView.frame = [[UIScreen mainScreen] bounds];
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多