ScrollView Add SubView in swift 3 - ios

I want to try add a view form on scroll view but view don't fully added on scroll view's frame, my code is:
import UIKit
class AddIncomeVC: UIViewController {
#IBOutlet var scrollView: UIScrollView!
#IBOutlet var views: UIView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.contentSize = views.frame.size
scrollView.addSubview(views)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
views.frame = CGRect(x: 0, y: 0, width: scrollView.frame.size.width, height: scrollView.frame.size.height)
}
}
Thanks,

Edit line
views.frame = CGRect(x: 0, y: 0, width: scrollView.frame.size.width, height: scrollView.frame.size.height)
to
views.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)

I get solved my issue with this code:
import UIKit
class AddIncomeVC: UIViewController {
#IBOutlet var scrollView: UIScrollView!
#IBOutlet var views: UIView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.contentSize = views.frame.size
scrollView.addSubview(views)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
views.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
}
}
Thanks all for giving valuable time for my question.

The problem is: At view did load, your constraints are not updated to the new display sizes.
I believe that your problem occurs because you are running the app using a simulator with different size from your storyboard... a bigger size (storyboard using iPhone SE and running at iphone 6 simulator, for example).
At View did Load, your view already exists, its true... but its constraints still have iphone SE sizes.
So, at view did load you are setting the content size (that you want with an iphone6 screen size) equal to storyboard view size (that still an iphone SE screen size).
Your solution works well. After the iOS layout all subviews, the constraints are updated to the new screen sizes, maintaining its proportions.
At viewDidLayoutSubviews the "view" already have the correct constraint values... So, the view has iphone6 screen sizes, and you could set the frame with the right

Related

Different look at iPhone 8 and iPhone X

There is a problem with the text view that's been programmatically created. It just pops up to ~20px at iPhoneX, but looks perfect at iPhone 8. I'll be super grateful for any hints :) Screenshots attached.
iPhoneX:
http://prntscr.com/lcnh2y
iPhone 8:
http://prntscr.com/lcnhns
Code:
let rect = CGRect(x: 20, y: self.tabsSegmentedControl.layer.position.y + 20, width: self.tabsSegmentedControl.layer.frame.width, height: 127)
self.ingredientsTextView = UITextView(frame: rect)
ingredientsTextView.isEditable = false
ingredientsTextView.isSelectable = false
self.view.addSubview(ingredientsTextView)
Use viewDidLoad for Creation and WillLayout for positioning.
I don't recommend mixing Storyboard and programmatically creation of the UI
class ViewController: UIViewController {
#IBOutlet weak var tabsSegmentedControl: UISegmentedControl!
var ingredientsTextView : UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
ingredientsTextView = UITextView(frame: .zero)
self.view.addSubview(ingredientsTextView)
ingredientsTextView.isEditable = false
ingredientsTextView.isSelectable = false
}
override func viewWillLayoutSubviews() {
let rect = CGRect(x: 20, y: self.tabsSegmentedControl.frame.maxY + 20, width: self.tabsSegmentedControl.frame.width, height: 127)
ingredientsTextView.frame = rect
ingredientsTextView.text = "There is a problem with the text view that's been programmatically created. It just pops up to ~20px at iPhoneX, but looks perfect at iPhone 8. I'll be super grateful for any hints :) Screenshots attached."
}
}

XIB isn't loaded properly in UIScrollView

I have a scrollview with paging. In this scroll view i load dynamically xibs. But the height of the xib should be the same as the scroll view. My problem is that the height of the xib is not loaded properly:
If I load a the xib file (which is almost fullscreen except on each border a constraint of 10) it isn't loaded properly because the height is too big.
So I have to make the constraint of bottom so that it fits into my scrollview:
Anyone got a solution?
Try to experiment with the code below. This general approach works for my project. Your problem is ambiguous definition for height property. Pay attention to find unnecessary constraints in your .xib.
#IBOutlet weak var yourCustomScrollView: UIScrollView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Firstly, define scrollview's position and size
yourCustomScrollView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
// Load .xib with custom class from main bundle.
guard let xib = Bundle.main.loadNibNamed("YourXibName", owner: self, options: nil)?.first as? YourCustomXibClass else {
fatalError("YourXibName is not found. 👎🏻")
}
self.yourCustomScrollView.addSubView(xib)
// Define .xib's position and size
xib.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
yourCustomScrollView.contentSize = CGSize(width: self.view.frame.width, height: xib.frame.height)
}

How to change button size?

I have button in my ViewController in storyboard. And I add some constraints to my button in storyboard. I want to change button size in code. But my code doesn’t work. How to fix it?
#IBOutlet var font: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
font = UILabel(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(50), height: CGFloat(20)))
}
If you connect you button from code with Interface Builder and set up constraints, you can change the size of button by changing constraints' constant.
class ViewController: UIViewController {
#IBOutlet var button: UIButton!
#IBOutlet var heightConstaint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
}
func foo() {
heightConstaint.constant = 50.0
view.setNeedsLayout()
}
}
If you don't use constraints, you can just change frame of view.
button.frame = CGRect(x: 0, y: 0, width: 50, height: 100)
You can update the button constraints, update from the code, or from the storyboard to take out the constraints, modify it
First you create button constraints Outlet . then modify the constraints like this:
self.buttonWidthConstaint.constant = 100.0

Make UIView fill UIScrollview

I have a paging scrollview with two views, and the first one is to fill the UIScrollview and the second to stay in the middle. I don't know what's going wrong, but it wouldn't fill on iPhone6 but, it fits fine on iPhone5. Below is my code:
View Hierarchy
UIView
--StackView (Properties -- Axis(Vertical) Alignment(Fill) Distribution(Fill) Spacing(0))
--UIView (Fixed height. 60)
--UIView (Main Views Holder)
--ScrollView(Content Size: Screen Width * 2, Height: Height left between it margin to the MainView and BottomView) and I notice, that the scrollview keeps remaining size 433 on both iPhone5 and 6. And when I check my storyboard, that's the exact same height there
--UIView(Height: 102, Width: Screen Width)BottomView
#IBOutlet weak var scrollView: UIScrollView!
var rectanglePhot: UIView!
var squarePhoto: UIView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.contentSize = CGSize(width: self.view.frame.size.width * 2, height: scrollView.frame.size.height)
scrollView.autoresizingMask = .FlexibleWidth
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.backgroundColor = UIColor.greenColor()
iniiCameraView()
}
func iniiCameraView() {
rectanglePhot = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.scrollView.frame.size.height)) <-- This is the problematic one
//rectanglePhot = UIView()
let specifyY = self.scrollView.frame.size.height - self.view.frame.size.width
squarePhoto = UIView(frame: CGRect(x: self.view.frame.size.width, y: specifyY, width: self.view.frame.size.width, height: self.view.frame.size.width))
scrollView.addSubview(rectanglePhot)
scrollView.addSubview(squarePhoto)
rectanglePhot.backgroundColor = UIColor.blueColor()
squarePhoto.backgroundColor = UIColor.brownColor()
print("Rect Height ", rectanglePhot.frame.size.height)
print("Scroll Height ", scrollView.frame.size.height)
print("Specify Y ", specifyY)
}
And the image Image Added to Scrollview a index[0]
Add the code in viewdidappear. The autolayout will be applied after viewdidload
Replace
UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.scrollView.frame.size.height))
With :
UIView(frame: CGRect(x: 0, y: 0, width: self.scrollView.bounds.size.width, height: self.scrollView.bounds.size.height))
With this the UIView will take the exact size of you scrollview
if you don't want to use autolayout: add subviews in viewDidLoad, that is fine but you also need to adjust their frames in viewDidLayoutSubviews method of UIViewController since your scrollView will have a different frame after autolayout passes.
if you are ok with using autolayout: don't rely on scrollView for sizing your subviews, relate size of your subviews to the size of self.view

Changing frame of UIView through IBOutlet from storyboard

#IBOutlet weak var outletView: UIView! //Referencing Outlet connected
override func viewDidLoad() {
super.viewDidLoad()
//not working
outletView.frame = CGRectMake(100, 100, 100, 100)
//working
var view = UIView();
view.frame = CGRectMake(0, 20, 100, 100);
self.view.addSubview(view);
}
How can I edit frame of #IBoutlet view as normal UIView instance?
I solved it to remove Use Size Classes on StoryBoard inspector.
When you set the frame in ViewDidload method , it first assigns the frame that you set inViewDidload and then the Storyboard Constraints are applied so your constraints are over-ridden.
In order to change the constraints apply the constraints in ViewDidAppear method and it will be reflected
override func viewDidAppear(animated: Bool)
{
outletView.frame = CGRect(x: 100, y: 100, width: 200, height: 400)
}

Resources