Xcode 6 Swift cannot input into textfield on scrollview - ios

This is my first time doing Xcode swift project, so please bear with me. I am trying to add a scroll view onto my input page. But after putting the text field on the scroll view, I cannot input anything on the text field any more. Please help me.
import UIKit
class AddInfoViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet var scrollView: UIScrollView!
var containerView2: UIView!
#IBOutlet var test2: UITextField!
#IBOutlet var test1: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let containerSize = CGSize(width: 300.0, height: 1000.0)
containerView2 = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size:containerSize))
scrollView.addSubview(containerView2)
//set up the minimum & maximum zoom scales
let scrollViewFrame = scrollView.frame
let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
let minScale = min(scaleWidth, scaleHeight)
scrollView.minimumZoomScale = minScale
scrollView.maximumZoomScale = 1.0
scrollView.zoomScale = 1.0
}

I think your ScrollView is covering all the things on your ViewController Like this way :
And you have to arrange it like this :
May be this can help you.

Are You Adding textField in storyboard or programmatically?
i think if you are adding scrollView Programmatically Then You also have to add textfield Programmatically! Or ScrollView Override UITextField Objects in storyBoard!
///////// if You Are Adding Scroll View in Xib ////////////////
in xib Click on "Show Document Outline"(on left Bottom Corner)!
Check
View
ScrollView
Round Style Text Field
Round Style Text Field
This shows that Your textField Are on ScrollView!
if You have it like this
View
Round Style Text Field
Round Style Text Field
ScrollView
Then Your TextField Are Override By Scroll View!

Related

Horizontal UIScrollView is not centering the current page

I would like to have a horizontal scroll layout which displays images. It works fine if setup 0, 0, 0 and 0 the constraints of the UIScrollView. The problem is exactly when I change the constraints to make margins surrounded the UIScrollView. This is what happens:
First image in the UIScrollView
Second image in the UIScrollView
Third image in the UIScrollView
As you can see, each time you scroll, more off-center the current page is.
I have tried to subtract trailing and leading constrains constants to the width of the scrollLayout, play with frames and bouds but without success.
If I run this example in a smaller display like iphone 5S, the problem is more pointed.
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var pageController: UIPageControl!
#IBOutlet weak var scrollView: UIScrollView!
let imagesArray = ["b_1", "b_2", "b_3", "b_4", "b_5"]
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.isPagingEnabled = true
self.pageController.numberOfPages = imagesArray.count
self.pageController.pageIndicatorTintColor = UIColor.blue
self.pageController.currentPageIndicatorTintColor = UIColor.gray
for i in 0...imagesArray.count - 1{
let imageView = UIImageView()
imageView.contentMode = .scaleToFill
imageView.image = UIImage(named: self.imagesArray[i])
let xPos = CGFloat(i)*self.view.bounds.size.width
imageView.frame = CGRect(x: xPos, y: 0, width: view.frame.size.width, height: self.scrollView.frame.size.height )
self.scrollView.contentSize.width = view.frame.size.width*CGFloat(i+1)
self.scrollView.addSubview(imageView)
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let page = scrollView.contentOffset.x/scrollView.frame.width
self.pageController.currentPage = Int(page)
}
}
So, I would like to know how to always obtain the current image centered.
Thank you
EDITED with Rajesh results and view debug:
I would recommend using a UICollectionView in place of a UIScrollView - otherwise you will be building a lot of the basics from scratch. You can use a collection view that centers the images, make sure paging is enabled and you should get the interface you're looking for. Make sure to adopt / conform to the UICollectionViewDelegate & UICollectionViewDelegateFlowLayout protocols with your view controller & set those delegates on your collection view. Hope that helps! Best of luck.

Paging with ScrollView using directly storyboard instead of creating .xib files

Sorry in advance, this is a long question but I wanted to explain as good as I can.
I need to implement a walkthrough(first time launch guide) for the project, which will only be shown to user for the first time they launch the app.
I've implemented it by creating a .xib file, where I was just creating a view for each item I need to implement for the walkthrough by creating objects referring to .xib model.
But now I'm required to implement it without using .xib files, where I need to do it via storyboard. This time I've inserted a scrollView, and then I put another view inside it with the objects I'm going to need such as (labels,imageViews,buttons etc.) But it doesn't work, even though I can create all the objects by copying the view right under the scrollView. When I try to expand the width of the scrollView with all the views created so I can do paging, it doesn't create each view next to each other but only shows me the last object(view) in the simulator as they add up vertically, and I can't do no paging.
Now I provide some sample code to show how I try to implement this:
import UIKit
class DenemeViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var pageControl: UIPageControl!
#IBOutlet weak var repeatingView: UIView!
var viewArray = [UIView]()
override func viewDidLoad() {
super.viewDidLoad()
let temporaryView = repeatingView
temporaryView?.backgroundColor = .black
viewArray.append(temporaryView!)
let temporaryViewTwo = repeatingView
temporaryViewTwo?.backgroundColor = .blue
viewArray.append(temporaryViewTwo!)
pageControl.numberOfPages = viewArray.count
pageControl.currentPage = 0
view.bringSubview(toFront: pageControl)
setupScrollView(items: viewArray)
}
func setupScrollView(items: [UIView]) {
scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(items.count), height: view.frame.height)
scrollView.isPagingEnabled = true
for i in 0..<items.count {
items[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(items[i])
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
pageControl.currentPage = Int(pageIndex)
}
}
As you can see I first create two different views in viewDidLoad by referring to "repeatingView" which is the outlet of the view inside scrollView on the storyboard. Then I return them in an array and try to implement scrollView's subviews with views.
I was expecting to see the scrollView starting with the view with black background and as I do the paging through right then the view with blue background should appear.
When I run this what I see is only the blue view, and I'm unable to do any paging or scrolling.

Navigation Bar on Scrollview bartint not on top

I'm working on a project, where I have multiple views on a scrollview.
I can scroll through them horizontally. It works nice!
But I've got a problem with the navigationbars in each views I added to the scrollview.
They are on it (as you can see from the screenshot), but the navigationbar on the top is not filling out everything from the top.
There's this white space, I don't want to have. I would like to have this space in the same blue as the navigationbar has.
Now my question, how can I achieve this?
I added the views with following code to the scrollview:
class ScrollViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let profilView = self.storyboard?.instantiateViewController(withIdentifier: "profilController") as! UIViewController
self.addChildViewController(profilView)
self.scrollView.addSubview(profilView.view)
profilView.didMove(toParentViewController: self)
profilView.view.frame = scrollView.bounds
let testViewTwo = self.storyboard?.instantiateViewController(withIdentifier: "chatController") as! UIViewController
self.addChildViewController(testViewTwo)
self.scrollView.addSubview(testViewTwo.view)
testViewTwo.didMove(toParentViewController: self)
testViewTwo.view.frame = scrollView.bounds
var frame:CGRect = testViewTwo.view.frame
frame.origin.x = self.view.frame.width
testViewTwo.view.frame = frame
self.scrollView.contentSize = CGSize(width: self.view.frame.width * 2, height: self.view.frame.height)
//startposition
//self.scrollView.contentOffset = CGPoint(x: (self.view.frame.width) * 1, y: self.view.frame.height)
}
Thanks for your help!
Set the Navigation Bar Height to 64 and place it "behind" the Status Bar.
Navigation Bar

scrollview autolayout issue xcode 8 with constraints

I am adding my intro screens in a scrollview.So I have to scroll horizontally only.I have set constraints for scrollview(top, bottom.leading, trailing).I have added subviews like the following
for i in 0..<self.arrIntro.count{
let view = IntroScreenView.instanceFromNib()
let x = CGFloat(i) * self.scrollIntro.frame.size.width
view.frame = CGRect(x: x, y: 0, width: self.scrollIntro.frame.size.width, height: self.scrollIntro.frame.size.height)
print(view,self.scrollIntro)
view.imgIntro.image = UIImage(named: "image-1")//UIImage(contentsOfFile: self.arrIntro[i]["Link"] as! String)
print(self.arrIntro[i]["Link"] as! String)
self.scrollIntro.addSubview(view)
}
self.scrollIntro.contentSize = CGSize(width: (CGFloat(self.arrIntro.count) * self.scrollIntro.frame.size.width ) , height: self.scrollIntro.frame.size.height)
but my scrollview is vertically scrolling little bit.How should I avoid this.Any autolayout issue?
You're not adding any constraints to your views. Add height and width constraints to each item, constraints between the views and constraints from the views to the scroll view. Note that the constraints between the Su views and the scroll view will set the content size for you.
Scroll view scrolls vertically, when contentSize.height is greater than the scrollView height.
Just make sure, your contentSize.height is not greater than the scrollView.frame.size.height.
One reason, that I could guess is that, you might be setting contentSize a point after which the frames of the scroll view changes. You should move setting you contentSize in viewDidLayoutSubviews()
Sample : This code makes vertical scroll
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.contentSize = CGSize(width: self.scrollView.bounds.width*5, height: self.scrollView.frame.height)
}
}
And this does not
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet var scrollView: UIScrollView!
override func viewDidLayoutSubviews() {
scrollView.contentSize = CGSize(width: self.scrollView.bounds.width*5, height: self.scrollView.frame.height)
}
}

ContentOffset doesn't do anything to UITextView the first time

I'm trying to have a UIViewController to display some text centered vertically in a UITextView. When app is launched, the text is aligned to the top. But if I try to select some text, the text would then move to the center vertically. It seems like setting contentOffset doesn't do anything at the first time, unless there's some action to trigger it to readjust the layout (like selecting some text in my scenario).
Also when I log the values of height, size, contentHeight and topCorrect, the values from the first call is the same as the second call. So I'm getting the same value but UITextView somehow is not responding to the contentOffset the very first time.
I even tried moving the logic to viewDidLayoutSubviews() and viewDidAppear(). I had the same result. So I don't think it's a problem of having the logic in the wrong place.
I think some people have success with it on iOS 7 and Obj-C. I'm trying get it running on iOS 8 and Swift.
Any suggestion or help would be very much appreciated.
class PageContentViewController : UIViewController {
#IBOutlet weak var textView: UITextView!
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.textView.textContainerInset = UIEdgeInsetsMake(0, 16.0, 0, 16.0)
let height = self.textView.bounds.size.height
let size = CGSizeMake(self.textView.frame.width, CGFloat.max)
var contentHeight = self.textView.sizeThatFits(size).height
var topCorrect = (height - contentHeight * self.textView.zoomScale) / 2.0
topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect;
self.textView.setContentOffset(CGPointMake(0, -topCorrect), animated: false)
}
}

Resources