【问题标题】:Background image for MoreNavigationControllerMoreNavigationController 的背景图片
【发布时间】:2010-08-23 07:55:46
【问题描述】:

我想在我的 UITabBarController moreNavigationController 中的 tableView 后面放置一个图像。在第一次设置 TabBar 时,我尝试过像这样插入子视图:

UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background3.png"]];
[self.tabBarController.moreNavigationController.topViewController.view insertSubview:imageView atIndex:0];

但这会将图像放在顶部,大概是因为 tableView 当时不存在。是否有更好的时间调用它以使其正常工作,或者更简单的方法?

【问题讨论】:

标签: iphone uinavigationcontroller


【解决方案1】:

this question 的帮助下,我想出了如何做到这一点。基本上,moreNavigationController中的viewController是一个单独的TableView,所以添加背景图片是行不通的。我需要做的是创建一个新视图,添加背景图像,然后在其上添加 moreNavigationController 视图。我通过在 UITabBarController 的子类中覆盖 viewDidLoad 来做到这一点,但我希望它也可以在其他地方完成。

- (void)viewDidLoad {
    [super viewDidLoad];

    UINavigationController *moreController = self.moreNavigationController;

    if ([moreController.topViewController.view isKindOfClass:[UITableView class]]) {
        UIView* newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,367)];

        UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background3.png"]];
        imageView.opaque = NO;
        imageView.alpha = 0.4;
        [newView addSubview:imageView];

        moreController.topViewController.view.backgroundColor = [UIColor clearColor];
        moreController.topViewController.view.frame = CGRectMake(0,0,320,367);
        [newView addSubview:moreController.topViewController.view];

        moreController.topViewController.view = newView;
    }
}

你可能在帧大小等方面更聪明,但这对我有用。希望它也对其他人有所帮助。

【讨论】:

    【解决方案2】:

    现在您可以从 UITableView 子类访问 backgroundView 属性。

        UIViewController *moreViewController = tabBarController.moreNavigationController.topViewController;
    
        img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BG_MORE+1.png"]];
    
    //Got some crashs in initialization !! Need to check .
    if ([moreViewController.view isKindOfClass:[UITableView class]]) {
        UITableView *moreTableView = (UITableView*)moreViewController.view;
        [moreTableView setBackgroundView:img];
    }
    

    【讨论】:

      【解决方案3】:

      除了这里乱七八糟的东西,你可以使用 UIView 的 bringSubviewToFront: 和 sendSubviewToBack: 来组织你的子视图。基本上这应该会有所帮助,尽管如果您有更多的子视图,则需要稍微尝试一下:

      [self.tabBarController.moreNavigationController.topViewController.view addSubview:imageView];
      [self.tabBarController.moreNavigationController.topViewController.view pushSubviewToBack:imageView];
      //or [self.tabBarController.moreNavigationController.topViewController.view bringSubviewToFront:tableView];
      

      【讨论】:

      • 我认为您的意思是“sendSubviewToBack”,恐怕这没有任何区别。我认为是因为在我调用代码时(在 applicationDidFinishLaunching 结束时),控制器尚未设置?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 2016-04-22
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多