If you’re developing an iOS application using UIWebView
, you may have noticed the default bounce or elastic scrolling behavior when users scroll beyond the content boundaries. While this effect feels natural in native apps, it might not be desirable in a web-based view—especially for fixed layouts or full-screen web apps.
In this guide, we’ll walk you through how to disable the bounce effect in UIWebView
, with a focus on Objective-C implementation and practical examples.
⚠️ Note: UIWebView is Deprecated
Before we dive in, it’s important to mention that UIWebView
has been officially deprecated by Apple since iOS 12 and replaced by the more powerful and efficient WKWebView
. If you’re starting a new project or updating an existing one, it’s strongly recommended to migrate to WKWebView
.
That said, if you’re maintaining legacy code, the following solution will still be helpful.
Why Disable Bounce in UIWebView?
The bounce effect in UIWebView
can:
- Distract users in fullscreen or kiosk-style apps
- Cause layout issues for fixed web content
- Affect the perceived performance and professionalism of your app
Disabling it ensures a more controlled and native-like experience for specific use cases.
Step-by-Step: Disable Bounce in UIWebView (Objective-C)
Here’s a simple and effective way to disable the bounce effect in UIWebView
:
for (UIView *subview in webView.subviews) {
if ([subview isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)subview;
scrollView.bounces = NO;
}
}
Where to Place This Code
You can add this snippet right after initializing and loading your UIWebView
, typically in viewDidLoad
:
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
NSURL *url = [NSURL URLWithString:@"https://example.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[self.view addSubview:webView];
// Disable bounce
for (UIView *subview in webView.subviews) {
if ([subview isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)subview;
scrollView.bounces = NO;
}
}
}
Bonus: How to Do It in WKWebView (Recommended for New Projects)
If you’re using WKWebView
, disabling the bounce is even simpler:
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
webView.scrollView.bounces = NO;
This is more straightforward and fully supported in modern iOS development.
Final Thoughts
Disabling the bounce effect in UIWebView
is a quick fix that enhances user experience in many apps. While it’s important to recognize that UIWebView
is deprecated, many legacy applications still use it and need optimizations like this one.
If you’re planning to future-proof your app, consider migrating to WKWebView
for better performance, modern features, and ongoing support.