Scroll view with UIWebView and labels - ios

I have a UIWebView with labels above it (see picture), but when I scroll down, the labels just stay in place and the web view scrolls down. How can I make everything scroll down in one motion ?
self.editorialDetailWebView.loadHTMLString(editorialArticleContentThroughSegue!, baseURL: nil)
self.editorialDetailWebView.sizeToFit()
self.editorialDetailScrollView.addSubview(editorialDetailWebView)
self.editorialDetailScrollView.addSubview(editorialDetailHeadlineLabel)
self.editorialDetailScrollView.addSubview(editorialDetailAuthorLabel)
self.editorialDetailScrollView.addSubview(editorialDetailPublishDateLabel)
self.editorialDetailScrollView.addSubview(editorialDetailVolumeAndIssueLabel)
let webViewHeight = self.editorialDetailWebView.scrollView.bounds.height
let webViewWidth = self.editorialDetailWebView.scrollView.bounds.width
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
editorialDetailScrollView.contentSize = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height * 2.0)
}
Screenshot of my issue now:

Add your views to the webview's scrollview
[self.webview.scrollView addSubview:self.myLabel];

Related

How to Make TableView scroll inside ScrollView behave naturally

I need to do this app. The view hierarchy goes like this
UIScrollView
-UIView (Main View)
--UIView (Top View Container)
--UITableview
When scrolling up the Main View, If table view has many cells, the table view should go to the top, and once it reaches the top. The user should be able to scroll the table view cells. I was able to achieve it but it doesn't scroll naturally.
Attached my code https://github.com/iamshimak/FinchHomeScreenRedesign.git
First, never put tableview inside a scrollview, it's a bad practice. You could just use tableview header and embed any type of view do you want before the tableview cells.
here's a snipeste on how I deal with it:
//MARK: ConfigureTableView
private func configureTableView(){
let footerView = UIView()
footerView.frame.size.height = 50
footerView.backgroundColor = .white
self.tableView.tableFooterView = footerView
self.tableView.tableHeaderView = self.headerView
var newFrame = headerView.frame
newFrame.size.width = view.bounds.width
newFrame.size.height = 300
headerView.frame = newFrame
tableView.backgroundView = UIView()
tableView.backgroundView?.addSubview(backgroundTableView)
}
as you can see, I embedded a UIView as a footer and another UIView named headerView as a header
but if you insist of using a tableview inside a scrollview, you can try using a scrollview delegate and detech which scrollview is scrolling
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let yOffset = scrollView.contentOffset.y
if scrollView == self.scrollView {
if yOffset >= scrollViewContentHeight - screenHeight {
// logic when using scrollview
}
}
if scrollView == self.tableView {
if yOffset <= 0 {
// logic when using tableview scrollView
}
}
}

Stop Vertical scrolling of WebView in Tableview header

One of the cells in a UITableView contains a scroll view. I want to be able to scroll the content in the cell horizontally, but NOT vertically.
How can I achieve this?
Additionally, the scroll view is a subview of UIWebView, so I cannot control its content size.
I have tried setting the content offset directly, but this prevents the entire table from being scrolled. I want the table to scroll vertically, but not the content in the cell.
here is code of I stop vertical scrolling of webview in scrollview delegate-
initialisation of web-view here -
let bodyTextView: WKWebView = {
// preferences
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
let configuration = WKWebViewConfiguration()
configuration.dataDetectorTypes = [.all]
// webview
let webView = WKWebView(frame: .zero, configuration: configuration)
webView.contentMode = .scaleToFill
webView.scrollView.showsVerticalScrollIndicator = false
webView.scrollView.tag = 2
webView.scrollView.delegate = self
return web
}()
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//here is code to disable vertical scrolling for webview if scroll view tag is equal to 2 set while initialising the webview.
if scrollView.tag ==2 {
DispatchQueue.main.async {
scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: 0)
}
}
//below code is used for Tableview-
let page = scrollView.contentOffset.x / view.frame.width
pageControl.currentPage = Int(page)
}

UIScrollView frame is changed by changing Simulator device

I have simple ViewController which displays images using UIScrollView which has constraints attaching it to the (top, leading, trailing) of the superView, and a UIPageControl, it works fine on iPhoneX simulator
When I run it on iPad Pro 9.7" simulator, the output is
After changing the View as attribute in the storyboard from iPhoneX to iPadPro 9.7" it worked well
This is the logic I use to calculate scrollviewContentSize & slidesSize
override internal func viewDidLoad() {
super.viewDidLoad()
tutorialScrollView.delegate = self
viewModel = TutorialViewModel()
configurePages()
setupSlideScrollView(slides: slides)
configurePageControl()
}
private func configurePages() {
if let viewModel = viewModel {
createSlides(tutotialPages: viewModel.getTutorialPages())
}
}
private func createSlides(tutotialPages: [TutorialPage]) {
for page in tutotialPages {
if let slide = Bundle.main.loadNibNamed(BUNDLE_ID, owner: self, options: nil)?.first as? TutorialSlideView {
slide.configure(title: page.title, detail: page.details, image: page.image)
slides.append(slide)
}
}
}
private func setupSlideScrollView(slides: [TutorialSlideView]) {
tutorialScrollView.contentSize = CGSize(width: view.frame.width * (CGFloat(slides.count)), height: tutorialScrollView.frame.height)
tutorialScrollView.isPagingEnabled = true
for i in 0 ..< slides.count {
slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: tutorialScrollView.frame.height)
tutorialScrollView.addSubview(slides[i])
}
}
Can anyone find the problem?
Did you try to print view's frame in setupSlideScrollView method to ensure it is correct? There is no guarantee that it will be correct in the viewDidLoad method if you use AutoLayout. Sometimes it will be, sometimes not. I assume in this particular case, it happened to be correct on iPhone X, but incorrect on iPad.
If that's the problem, you should set contentSize and slides' frames in viewDidLayoutSubviews. Adding slides as subviews should stay in viewDidLoad/setupSlideScrollView because viewDidLayoutSubviews usually gets called multiple times.

UITextView with other elements (similar to Evernote) iOS

I'm trying to implement a view very similar to Evernote's screen in which you add a New Note.
It seems like a UITableView embedded in a NavigationController. This tableview contains static cells (2 or 3) with the bottom one being a UITextView in which you add the content of the note, but when you scroll on the textView, the other cells that contain a textField and another control.
How can this be achieved? I know that Apple doesn't recommend a TextView inside a ScrollView, and doing it with table view it gets a bit weird with all the scrolling from the table and text view.
Here are some examples:
Any suggestions?
Thank you!
Firstly, They disabled text view scrolling and set its size to about screen size. Secondly, once text view's text is out of frame, expand it(calculate its size again).
So I found my problem, when I was setting the constraints for the content view (view inside scrollview) I set an Equal value for its height. To fix it I just made that relationship to Greater or Equal than... it now expands.
The other problem now is that when showing the keyboard it is not scrolling to the text I tap to. (The insets are properly setup though)
// MARK: Notififations from the Keyboard
func didShowKeyboard (notification: NSNotification) {
if momentTextView.isFirstResponder() {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.size.height, right: 0)
scrollView.scrollIndicatorInsets = scrollView.contentInset
let caretPosition = momentTextView.caretRectForPosition(momentTextView.selectedTextRange!.start)
let newHeight = caretPosition.height * 1.5
let newCaretPosition = CGRect(x: caretPosition.origin.x, y: caretPosition.origin.y, width: caretPosition.width, height: newHeight)
scrollView.scrollRectToVisible(newCaretPosition, animated: true)
}
}
}
func willHideKeyboard (notification: NSNotification) {
if momentTextView.isFirstResponder() {
scrollView.contentInset = UIEdgeInsetsZero
scrollView.scrollIndicatorInsets = UIEdgeInsetsZero
}
}

conflict scrolling scrollview and tableview

I have a scrollview that contain some element (uiimage, webview ,...)
in buttom of scrollview add tableview (comments list). Problem: although tableview is part of scrollview, but scrollview scroll separate and tableview scrolling separate!
I want at the end of scrollview and start tableview scrollview scrolling tableview and tableview scroll disabled.
I used it code:
Swift:
override func intrinsicContentSize() -> CGSize {
self.layoutIfNeeded()
return CGSizeMake(UIViewNoIntrinsicMetric, contentSize.height)
}
Objective C:
-(CGSize)intrinsicContentSize{
[self layoutIfNeeded];
return CGSizeMake(UIViewNoIntrinsicMetric, contentSize.height)}
but don't work.
thanks for help
That happens because it is the behavior of having a table view inside a scroll view. That should be happening.
Solution: Destroy the scrollView, and implement a tableView with a header view, wish that header view it will be the view with uiimage, webview etc... and the tableView it will be your comments. This is the best way of implementing what you want, that is if i understood right what you actually want.
Adding a header to a table view example:
self.tableView.tableHeaderView = topView // where top view is the view wish contains your uimage, buttons etc...
Avoid bounce of ScrollView when we scroll the tableview. I have added the below line of code.It worked for me.
self.scrollView.delegate = self
scrollView.contentSize = CGSize(width: self.view.frame.width, height: 500)
scrollView.showsVerticalScrollIndicator = true
scrollView.tag = 1
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView.tag != 1 {
self.scrollView.bounces = false
}
}
Thanks

Resources