UIScrollView doesn't scroll and doesn't appear on the right position - ios

I need a UIScrollView and a containerView to scroll the content I create. My code for that 2 items is:
var scrollView = UIScrollView()
var darkener = UIView()
var container = UIView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.clearColor()
darkener = UIView(frame: self.view.frame)
darkener.backgroundColor = UIColor.blackColor()
darkener.alpha = 0.0
self.view.addSubview(darkener)
scrollView.frame = self.view.bounds
scrollView.backgroundColor = UIColor.clearColor()
scrollView.contentSize = CGSizeMake(scrollView.frame.width, scrollView.frame.height+1)
scrollView.delegate = self
self.view.addSubview(scrollView)
container = UIView(frame: CGRectMake(0, scrollView.frame.height, scrollView.frame.width, scrollView.frame.height))
container.backgroundColor = UIColor.whiteColor()
container.layer.masksToBounds = true
container.layer.cornerRadius = 10
scrollView.addSubview(container)
}
The problem is that the container view once I Set the Y point to scrollView.frame.height doesn't appear on the screen (it's on the bottom), but in a previous VC that code worked perfectly.
Also, it does not let me scroll on the content. What's wrong there?
If necessary I can upload the implementation, but this I think has no problem.

Solved. I just had to put that at the end of the ViewDidLoad to declare de frame:
The textField is the final label I have on the screen, to declare the limit of the screen.
self.container.frame = CGRectMake(0, self.container.frame.origin.y, self.container.frame.width, textField2.frame.origin.y + textField2.frame.height - 60)
self.scrollView.contentSize = CGSizeMake(self.container.frame.width, self.container.frame.height + 20.0)
And works Ok now.

Related

UIScrollView for ViewController horizontally and vertically

My code tutorial works for scrolling horizontally with ScrollView.
I would like to make it with ViewControllers (instead of views) from my Storyboard.
And I would like to add a vertical ScrollView (To move from center to right or left = ScrollView horizontal, and to move from center to top or bottom (vertically).
Is that possible? Could someone give me a hint ?
Thanks
For information, this is my code :
import UIKit
class ViewController: UIViewController {
lazy var view0: UIView = {
let view = UIView()
view.backgroundColor = .systemTeal
let label = UILabel()
label.text = "Page 0"
label.textAlignment = .center
view.addSubview(label)
label.edgeTo(view: view)
return view
}()
lazy var view1: UIView = {
let view = UIView()
view.backgroundColor = .systemPink
let label = UILabel()
label.text = "Page 1"
label.textAlignment = .center
view.addSubview(label)
label.edgeTo(view: view)
return view
}()
lazy var view2: UIView = {
let view = UIView()
view.backgroundColor = .systemYellow
let label = UILabel()
label.text = "Page 2"
label.textAlignment = .center
view.addSubview(label)
label.edgeTo(view: view)
return view
}()
lazy var views = [view0, view1, view2]
lazy var scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.showsHorizontalScrollIndicator = false
scrollView.isPagingEnabled = true
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(views.count), height: view.frame.height)
for i in 0..<views.count {
scrollView.addSubview(views[i])
views[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
}
scrollView.delegate = self
return scrollView
}()
lazy var pageControl: UIPageControl = {
let pageControl = UIPageControl()
pageControl.numberOfPages = views.count
pageControl.currentPage = 0
pageControl.addTarget(self, action: #selector(pageControlTapHandler(sender:)), for: .touchUpInside)
return pageControl
}()
#objc
func pageControlTapHandler(sender: UIPageControl) {
scrollView.scrollTo(horizontalPage: sender.currentPage, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.addSubview(scrollView)
scrollView.edgeTo(view: view)
view.addSubview(pageControl)
pageControl.pinTo(view)
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x / view.frame.width)
pageControl.currentPage = Int(pageIndex)
}

Programmatically adding UILabels and UIButtons to UIScrollView in Swift

My goal is to programmatically create multiple UILabels and UIButtons followed by programmatically creating and inserting them into a UIScrollView.
I am operating within the following class.
class ViewController: UIViewController {
I have first created my scrollView shown below.
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
createTitleBar() //Function that creates fixed buttons and labels not included in scrollView
createUpperTabs() //Function that creates fixed buttons and labels not included in scrollView
createLowerNavBars() //Function that creates fixed buttons and labels not included in scrollView
for _ in 0...10 {
createTemplate() //This function consists of nested functions that creates labels and buttons that are then added to the scrollView
}
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.black
scrollView.contentSize = CGSize(width: screenSize.width, height: currentStackY)
scrollView.autoresizingMask = UIViewAutoresizing.flexibleHeight
view.addSubview(scrollView)
}
After creating the labels and buttons, I add the views individually to the scrollView with following statement
self.scrollView.addSubview(UILabel or UIButton Name).
Here are some snippets showing how I am creating my UILabel and UIButton.
let titleHeader = UILabel()
self.scrollView.addSubview(titleHeader)
currentStackY += 8
titleHeader.backgroundColor = .gray
titleHeader.text = headerTitle
titleHeader.font = UIFont(name: buttonFont, size: buttonFontSize)
let labelWidth = titleHeader.intrinsicContentSize.width + 16
titleHeader.snp.makeConstraints { (make) in
make.height.equalTo(titleHeader)
make.width.equalTo(titleHeader)
make.top.equalTo(currentStackY)
make.left.equalTo(view).offset(8)
}
as well as
for i in 0...chiefComplaintArray.count - 1 {
let btn = UIButton()
chiefComplaintButton.append(btn)
buttonDictionary[chiefComplaintButton[i]] = numberButtonPressed
chiefComplaintButton[i].setTitle(chiefComplaintArray[i], for: .normal)
chiefComplaintButton[i].setTitleColor(UIColor.black, for: .normal)
chiefComplaintButton[i].titleLabel!.font = UIFont(name: buttonFont, size: buttonFontSize)
chiefComplaintButton[i].backgroundColor = UIColor.gray
chiefComplaintButton[i].sizeToFit()
let buttonWidth = chiefComplaintButton[i].frame.width + 10
let buttonHeight = chiefComplaintButton[i].frame.height + 5
if i == 0 {
currentStackX = 20
currentStackY += 5
} else if (currentStackX + buttonWidth) > screenSize.width {
currentStackX = 20
currentStackY += buttonHeight + 5
}
chiefComplaintButton[i].frame = CGRect(x: currentStackX, y: currentStackY, width: buttonWidth, height: buttonHeight)
currentStackX += chiefComplaintButton[i].frame.width + 5
if i == chiefComplaintArray.count - 1 {
currentStackY += chiefComplaintButton[i].frame.height
}
self.scrollView.addSubview(chiefComplaintButton[i])
chiefComplaintButton[i].addTarget(self, action: #selector(self.buttonPressed), for: .touchUpInside)
}
I used similar statements to add all of the created content to the scrollView. I have modeled my UIScrollView after other solutions in the stacks as well as other online references. Thanks in advance for your time and consideration.
I have subsequently tried
let titleHeader = UILabel()
scrollView.addSubview(titleHeader)
self.view.addSubview(scrollView)
currentStackY += 8
titleHeader.backgroundColor = .gray
titleHeader.text = headerTitle
titleHeader.font = UIFont(name: buttonFont, size: buttonFontSize)
let labelWidth = titleHeader.intrinsicContentSize.width + 16
Still getting the same error.
The issue with your code is you are initializing your scrollView after you are calling the method in which you are getting crash,because at that time your scrollView is nil.
You need to call your method after
view.addSubview(scrollView)
Edit
override func viewDidLoad() {
super.viewDidLoad()
for _ in 0...10 {
createTemplate() //This function consists of nested functions that creates labels and buttons that are then added to the scrollView
}
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.black
scrollView.contentSize = CGSize(width: screenSize.width, height: currentStackY)
scrollView.autoresizingMask = UIViewAutoresizing.flexibleHeight
view.addSubview(scrollView)
createTitleBar() //Function that creates fixed buttons and labels not included in scrollView
createUpperTabs() //Function that creates fixed buttons and labels not included in scrollView
createLowerNavBars() //Function that creates fixed buttons and labels not included in scrollView
}
Also from here
let titleHeader = UILabel()
scrollView.addSubview(titleHeader)
self.view.addSubview(scrollView)
currentStackY += 8
titleHeader.backgroundColor = .gray
titleHeader.text = headerTitle
titleHeader.font = UIFont(name: buttonFont, size: buttonFontSize)
let labelWidth = titleHeader.intrinsicContentSize.width + 16
remove this
self.view.addSubview(scrollView)

UITextField not responds to touch events when added to UIView

I have an issue with UITextField that do not respond to the touch events when it's added to ContainerView. When I try to add UITextField to the main UIViewController it seems to be fine and it responds (keyboard is showing).
I tried to bring the subview to the front, enabled user interaction on ContainerView - nothing helped.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.scrollView = UIScrollView()
self.containerView = UIView()
self.scrollView.delegate = self
self.containerView.userInteractionEnabled = true
self.scrollView.addSubview(containerView)
self.view.addSubview(scrollView)
registerForKeyboardNotifications()
setupGesturesAndMap()
setupActivityIndicator()
setupCircleButton()
setupBrowseTextField()
// Initializing status
status = Status.Init
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.frame = self.view.bounds
containerView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)
}
and there is function responsible for setup of browseTextField:
func setupBrowseTextField() {
// Setup of browseTextField
browseTextField = UITextField(frame: CGRectMake(10, self.view.frame.height / 2, self.view.frame.width - 20, self.view.frame.height / 12))
browseTextField.placeholder = "Enter your destination here"
browseTextField.font = UIFont.systemFontOfSize(15)
browseTextField.borderStyle = UITextBorderStyle.RoundedRect
browseTextField.autocorrectionType = UITextAutocorrectionType.No
browseTextField.keyboardType = UIKeyboardType.Default
browseTextField.returnKeyType = UIReturnKeyType.Done
browseTextField.clearButtonMode = UITextFieldViewMode.WhileEditing
browseTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
browseTextField.delegate = self
containerView.addSubview(browseTextField)
}
Would anyone of you have any suggestions?
I see you have set the frame of the containerView as
containerView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)
scrollView.contentSize.width is 0.
This solves your problem.
containerView.frame = self.view.bounds
Also your can give the containerView size by the frame size of the scrollView as
containerView.frame = CGRectMake(0, 0, scrollView.frame.width, scrollView.frame.height)

ImageView and Label not filling parent (UIScrollView)

I have a UIScrollView and I am trying to add a UIImageView inside the scrollview. But after that, I want a UILabel inside the imageview. Basically, the image should fill the scroll view and the label should appear in the center of the image. Here is what I have:
#IBOutlet weak var image_scroll_view: UIScrollView!
var imageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
image_scroll_view.backgroundColor = UIColor.yellowColor()
imageView.frame = CGRectMake(0, 0, image_scroll_view.frame.size.width, image_scroll_view.frame.height)
imageView.userInteractionEnabled = true
imageView.contentMode = UIViewContentMode.ScaleAspectFit
imageView.backgroundColor = UIColor.greenColor()
image_scroll_view.scrollRectToVisible(imageView.frame, animated: true)
self.imageView.image = // ...
let curr_user_name = UILabel(frame: CGRectMake(0, 0, imageView.frame.width, imageView.frame.height))
curr_user_name.text = "John Smith"
curr_user_name.textColor = UIColor.whiteColor()
curr_user_name.font = UIFont.boldSystemFontOfSize(16.0)
curr_user_name.backgroundColor = UIColor.redColor()
curr_user_name.center = CGPointMake(imageView.center.x, imageView.center.y)
curr_user_name.textAlignment = NSTextAlignment.Center
imageView.addSubview(curr_user_name)
image_scroll_view.addSubview(imageView)
self.navigationController?.navigationBarHidden = true
}
Here is what it looks like:
The scrollview is yellow. Ideally, the image view should fill all of that (it doesn't even show up at the moment) and the label should fill all of the image view, which would automatically center the text.
What am I doing wrong here?
make cur_user_name a property and
Write this code:
imageView.frame = CGRectMake(0, 0, image_scroll_view.frame.size.width, image_scroll_view.frame.height)
curr_user_name.center = CGPointMake(imageView.center.x, imageView.center.y)
in viewDidLayoutSubviews

UILabel resizes its SuperView?

I have the following view which contains a UILabel:
class MyView : UIView {
func viewDidLoad() {
self.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
bottomView = UIView(frame: CGRectMake(self.bounds.origin.x, self.bounds.origin.y + self.imageView!.bounds.size.width, self.bounds.size.width, self.bounds.size.height - self.imageView!.bounds.size.height))
// bottomView frame calculation is: (0.0, 355.0, 355.0, 130.0)
bottomView?.backgroundColor = UIColor.greenColor()
bottomView?.autoresizingMask = UIViewAutoresizing.FlexibleWidth
bottomView?.clipsToBounds = true
self.addSubview(self.bottomView!)
var descriptionRect: CGRect = CGRectInset(self.bottomView!.bounds, leftRightInset, 20/2)
let descriptionLabel = UILabel()
descriptionLabel.numberOfLines = 3
descriptionLabel.autoresizingMask = UIViewAutoresizing.FlexibleWidth
descriptionLabel.font = UIFont(name: MGFont.helvetica, size: 22)
descriptionLabel.textColor = UIColor.whiteColor()
descriptionLabel.textAlignment = NSTextAlignment.Left
descriptionLabel.backgroundColor = UIColor.blueColor()
var paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 1.0
paragraphStyle.lineBreakMode = NSLineBreakMode.ByTruncatingTail
let attributes = [NSParagraphStyleAttributeName : paragraphStyle]
descriptionLabel.attributedText = NSAttributedString(string: previewCard.title, attributes:attributes)
bottomView?.addSubview(descriptionLabel)
descriptionLabel.bounds = descriptionRect
descriptionLabel.sizeToFit()
descriptionLabel.center = CGPointMake(bottomView!.bounds.width/2, bottomView!.bounds.height/2 - hotelNameLableHeight/2)
}
}
The height of the bottomView should always be fixed.
MyView is resized during runtime. This means that the green bottom view also increases in size.
Here is the result when the label has two and three lines:
It appears that the UILabel resizes its super view.
Note that I do not use AutoLayout.
override func layoutSubviews() {
super.layoutSubviews()
}
How can I prevent the UILabel from resizing its SuperView?
Edit: I also tried to comment bottomView?.clipsToBounds = true
Override setFrame: and setBounds: of the super view (subclass if they're plain UIViews), add breakpoints, and see the stack trace to find out what's causing them to resize.
There is no need to set the autoResizingMask on the label, just set the frame and it will get automatically centered. And of course you can set the insets for the UILabel accordingly. I've add below testing code FYI:
override func viewDidLayoutSubviews() {
addTestView(CGRectMake(0, 200, view.bounds.width, 50), labelStr: "I am a short testing label")
addTestView(CGRectMake(0, 260, view.bounds.width, 50), labelStr: "I am a very longlonglonglonglonglonglong testing label")
addTestView(CGRectMake(0, 320, view.bounds.width, 50), labelStr: "I am a very longlonglonglonglonglonglonglonglonglonglonglong testing label. Will be truncated")
}
func addTestView(frame:CGRect, labelStr: String){
let bottomView = UIView(frame:frame)
bottomView.backgroundColor = UIColor.greenColor()
bottomView.autoresizingMask = UIViewAutoresizing.FlexibleWidth
bottomView.clipsToBounds = true
view.addSubview(bottomView)
var label = UILabel(frame: bottomView.bounds)
label.textAlignment = NSTextAlignment.Left
label.numberOfLines = 0
label.text = labelStr
bottomView.addSubview(label)
}

Resources