---
metadata:
  - name: generator
    content: Diplodoc Platform v5.50.6
alternate:
  - https://ads.yandex.com/helpcenter/en/dev/ios/slider.md
  - https://ads.yandex.com/helpcenter/ru/dev/ios/slider.md
  - https://ads.yandex.com/helpcenter/zh/dev/ios/slider.md
---
> **Documentation Index:** Fetch the complete configuration index at https://ads.yandex.com/helpcenter/zh/llms.txt

# 广告滑块

<!-- source: zh/dev/_includes/native-ads.md -->
原生广告是一种布局可以在应用级别定义的广告类型。这一功能允许您根据应用设计的具体上下文更改广告的视觉风格以及投放位置。
<!-- endsource: zh/dev/_includes/native-ads.md -->

<!-- source: zh/dev/_includes/native-ads.md -->
广告呈现通过本地平台工具执行，从而提高广告效果和质量。
<!-- endsource: zh/dev/_includes/native-ads.md -->

<!-- source: zh/dev/_includes/native-ads.md -->
原生广告可以增强广告体验。因此，您可以在不失去用户兴趣的情况下展示更多广告。从长远来看，这确保了广告收入的最大化。
<!-- endsource: zh/dev/_includes/native-ads.md -->

## 前提条件 {#pre}

<!-- source: zh/dev/_includes/pre-ios.md -->
1. 请按照 [快速入门](https://ads.yandex.com/helpcenter/zh/dev/ios/quick-start.md) 中描述的 SDK 集成步骤进行操作。
2. 提前 [初始化](https://ads.yandex.com/helpcenter/zh/dev/ios/quick-start.md#init) 您的广告 SDK。
3. 确保您运行的是最新的 [Yandex Mobile Ads SDK](https://ads.yandex.com/helpcenter/zh/dev/platforms.md) 版本。如果您使用聚合，请同时确保您运行的是最新版本的 [统一构建](https://ads.yandex.com/helpcenter/zh/dev/platforms.md)。
<!-- endsource: zh/dev/_includes/pre-ios.md -->

## 加载滑块 {#load}

1. 创建 `NativeAdLoader` 类的实例以获取滑块中的广告。

2. 使用 `NativeAdRequestConfiguration` 类创建 `nativeAdRequestConfiguration`。您可以使用广告单元 ID、加载图像的方法、年龄、性别以及其他可能提高广告选择质量的数据作为请求参数。

3. 设置用于检索实施 `NativeAdLoaderDelegate` 协议的广告的委托：

4. 要跟踪广告加载过程，请实施 `NativeAdLoaderDelegate` 协议方法：`-nativeAdLoader:didFailLoadingWithError:` 和 `-nativeAdLoader:didLoadAd:`。

5. 要加载广告，请将 `loadAdWithRequestConfiguration:` 消息发送到加载器。

6. 如果广告已加载，则会调用以下方法：
   ```swift
   func nativeAdLoader(_ loader: NativeAdLoader, didLoad ad: NativeAd)
   ```

7. 如果广告未加载，则会调用以下方法：
   ```swift
   func nativeAdLoader(_ loader: NativeAdLoader, didFailLoadingWithError error: Error)
   ```

{% cut "一般广告请求示例" %}

```swift
// 创建加载器
adLoader = NativeAdLoader()
adLoader.delegate = self

// 创建请求配置
let requestConfiguration = NativeAdRequestConfiguration(adUnitID: "<AdUnitID>")

// 将请求配置传递给加载器
adLoader.loadAd(with: requestConfiguration)

// 实施委托方法
// ...

func nativeAdLoader(_ loader: NativeAdLoader, didLoad ad: NativeAd) {
    // 呈现广告
}
```

{% endcut %}

## 呈现原生广告滑块 {#ad-view}

加载滑块后，您必须呈现其所有资产。

成功加载的滑块包含一个或多个具有相同上下文的原生广告。滑块中的广告必须显示在同一个共享容器中：否则，广告展示次数将不会计入。

### 手动设置原生广告滑块的布局 {#config}

当模板设置不足以满足您的需求时，您可以手动配置原生广告滑块的布局。

使用此方法，您可以通过相对定位广告组件来安排原生广告的滑块。您的广告可能包含强制和可选的显示资产。您可以在 [原生广告资产](https://ads.yandex.com/helpcenter/zh/dev/ios/components.md) 中找到它们的完整列表。

{% note tip %}

对于滑块中的每个广告，我们建议使用包含一整套可能资产的布局。经验表明，具有完整资产集的布局更易被点击。

{% endnote %}

调用 `bindAdToSliderView` 方法并将广告滑块的容器传递给它。

滑块中的每个广告均使用标准原生广告 [布局方法](https://ads.yandex.com/helpcenter/zh/dev/ios/native.md#ad-view) 进行布局。

```swift
    func nativeAdLoader(_ loader: NativeAdLoader, didLoad ad: NativeAd) {
        self.ad = ad
        ad.delegate = self
        // 检查嵌套广告
        if ad.ads.count != 0 {
            // 为滑块创建一个容器；使用此类的子类替换 YMANativeAdView。
            let sliderAdView = YMANativeAdView()

            // 调用方法 bindAd(toSliderView: _) 并将容器传递给它
            do {
                try ad.bindAd(toSliderView: sliderAdView)
            } catch {
                // 检查错误消息并修复问题
                return
            }

            for subAd in ad.ads {
                // 订阅委托
                subAd.delegate = self

                // 为广告创建广告视图
                // 使用此类的子类替换 YMANativeAdView
                let subAdView = YMANativeAdView()

                // 针对广告调用 bind(with: subAdView) 方法
                do {
                    try subAd.bind(with: subAdView)
                } catch {
                    // 检查错误消息并修复问题
                    return
                }

                // 将广告添加到容器中
                sliderAdView.addSubview(subAdView)
                // 配置广告在容器中的版位
            }

        } else {
            // 作为常规原生广告处理
        }
    }
```

