简体   繁体   English

子视图不与超级视图混合

[英]subview not blending with superview

I want to include a searchbar in the navigation bar of a scene. 我想在场景的导航栏中包含搜索栏。

Using this code, I successfully put the searchbar in the navigation bar. 使用此代码,我成功地将搜索栏放置在导航栏中。

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(35, 0, 150, 40)];
    self.navigationItem.titleView = titleView;

    UISearchBar *mynewsb = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,150,40)];

    mynewsb.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
    mynewsb.opaque = NO;

    [self.navigationItem.titleView addSubview:mynewsb];
}

However, when I run the app, I get this. 但是,当我运行该应用程序时,我得到了。

在此处输入图片说明

You can see the background of the search bar is still discernable. 您可以看到搜索栏的背景仍然清晰可见。 It has not blended with the titleView. 它尚未与titleView融合。 BTW the two bar button items were added using a storyboard. 顺便说一句,使用情节提要添加了两个条形按钮项目。

I tried these codes but no success. 我尝试了这些代码,但没有成功。

mynewsb.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
mynewsb.opaque = NO;

What can I do in order to blend the search bar background nicely with the titleView? 为了使搜索栏背景与titleView完美融合,该怎么办?

Height of Title Bar is 44. 标题栏的高度为44。

So use CGRectMake(35, 0, 150, 44) or CGRectMake(0,0,150,44) instead of 40. 因此,请使用CGRectMake(35,0,150,44)或CGRectMake(0,0,150,44)而不是40。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM