You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 9中UIActivityViewController视图Frame被截断问题求助

Fixing UIActivityViewController Frame Truncation on iOS 9

Hey there, let's tackle this UIActivityViewController frame truncation bug you're hitting on iOS 9! It's super frustrating when a feature works great on newer iOS versions but breaks on older ones—let's break down what's going on and how to fix it.

First, let's fill out the code snippet you provided (since it cut off mid-way) to make sure we're aligned:

NSString *url = @"http://itunes.apple.com/us/app/APPNAME/idXXXXXXXXX";
NSString *title = [NSString stringWithFormat:@"Download ECG app %@ and get free reward points!", url];
NSArray *activityItems = @[title, [NSURL URLWithString:url]]; // Assuming this is your full activity items array

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

// Popover setup for iPad (critical for iOS 9 layout)
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    activityVC.popoverPresentationController.sourceView = self.view;
    activityVC.popoverPresentationController.sourceRect = sender.frame; // Replace with your trigger button's frame
}

[self presentViewController:activityVC animated:YES completion:nil];

Why This Happens on iOS 9

iOS 9 handles UIActivityViewController's layout differently than iOS 10+. The biggest culprit here is long, unbroken text in your share items—your title string includes a full URL, making it extra lengthy, which throws off iOS 9's automatic layout calculations. Additionally, missing or incorrect popover presentation settings (even on iPhone, in edge cases) can contribute to frame truncation.

Solutions to Try

1. Split Share Content (Cleanest Fix)

Instead of embedding the URL directly into your title string, pass the title and URL as separate activity items. iOS handles NSURL objects natively in share sheets, which avoids long-text layout glitches:

NSString *urlStr = @"http://itunes.apple.com/us/app/APPNAME/idXXXXXXXXX";
NSString *title = @"Download ECG app and get free reward points!";
NSURL *shareURL = [NSURL URLWithString:urlStr];
NSArray *activityItems = @[title, shareURL];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

// Keep your popover setup here
[self presentViewController:activityVC animated:YES completion:nil];

This lets the share sheet properly format each item without cramming everything into one long text block, fixing most iOS 9 layout truncation issues.

2. Force Preferred Content Size for iOS 9

If splitting the content doesn't work, explicitly set the preferredContentSize for iOS 9 devices to override broken automatic layout:

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

// Set preferred size only for iOS 9
if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0) {
    // Adjust height as needed—400 is a safe starting point
    activityVC.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 400);
}

// Popover setup and present code here

3. Adjust Frame After Presentation

As a last resort, manually correct the frame after presenting the share sheet:

[self presentViewController:activityVC animated:YES completion:^{
    if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0) {
        activityVC.view.frame = [UIScreen mainScreen].bounds;
        [activityVC.view setNeedsLayout];
        [activityVC.view layoutIfNeeded];
    }
}];

This forces the share sheet to take up the full screen, eliminating truncation.

Bonus: Check Custom Activities

If you're using custom UIActivity subclasses, make sure their layout doesn't exceed expected sizes on iOS 9. Custom activities with oversized views can push the share sheet's layout out of bounds.


内容的提问来源于stack exchange,提问作者Tan Nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:37:09