【问题标题】:Placing iAD banner always at the bottom of the screen(IOS 7)始终将 iAD 横幅放置在屏幕底部(IOS 7)
【发布时间】:2014-02-24 14:09:04
【问题描述】:

所以我正在实现单例 iAD 系统,并获得了在视图上添加横幅的功能,但我如何确保横幅始终位于底部,以便它适用于 3.5 英寸和 4 英寸屏幕?

这是我的代码,但横幅无法正确定位,它会离开屏幕

-(void)attachBannerToViewController:(UIViewController*)viewController
{

    if (_iAdBanner == nil) {
        NSLog(@"iAdbanner alloc");
        _iAdBanner = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
        _iAdBanner.delegate = self;
        _iAdBanner.alpha = 0.0;
    }

    CGRect frame=CGRectZero;
    frame.size = _iAdBanner.frame.size;
    frame.origin = CGPointMake(0.0, viewController.view.frame.size.height-_iAdBanner.frame.size.height);

    [_iAdBanner setFrame:frame];
    [viewController.view addSubview:_iAdBanner];
}

【问题讨论】:

    标签: ios cocoa-touch uiview ios7 iad


    【解决方案1】:

    猜测,我会说您在查看控制器完成所有设置之前运行此代码。当您运行这行代码时,它将是正确的,但当view controller.view 被添加到window 时,它就会出错。

    考虑设置自动调整大小的标志或约束,以将广告横幅保持在正确的位置。你用的是哪个?

    _iAdBanner.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth;

    约束....其他人刚刚发布并回答设置约束,所以我不会重复他们的工作。 :)

    【讨论】:

    • 如果你要使用自动调整大小的蒙版,你可能还想给它灵活的宽度,这样它就可以填满视图控制器的整个宽度
    • 虽然这通常是一件合适的事情,但它会为答案增加额外的复杂性,并且会做一些没有要求甚至可能不需要的事情。如果人们希望该行为提供的代码位很可能会为他们提供足够的信息来调整其他所需行为的行。
    【解决方案2】:

    你可以使用NSLayoutConstraint:

    -(void)attachBannerToViewController:(UIViewController*)viewController
    {
        if (_iAdBanner == nil) {
            NSLog(@"iAdbanner alloc");
            _iAdBanner = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
            _iAdBanner.delegate = self;
            _iAdBanner.alpha = 0.0;
            _iAdBanner.translatesAutoresizingMaskIntoConstraints = NO;
            [viewController.view addSubview:_iAdBanner];
            [viewController.view addConstraints:
             [NSLayoutConstraint
              constrainsWithVisualFormat:@"|-0-[banner]-0-|"
              options:0
              metrics:nil
              views:@{"banner" : _iAdBanner}]];
            [viewController.view addConstraints:
             [NSLayoutConstraint
              constrainsWithVisualFormat:@"V:[banner]-0-|"
              options:0
              metrics:nil
              views:@{"banner" : _iAdBanner}]];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      相关资源
      最近更新 更多