I found that placing a view in my app and sizing it to my needs is pretty similar to divs in HTML. Should I be using them this way?
Here's an example of a place I want to use a view.
I want to fill that in, should I be using a view here? or something more semantical?
End result with a view:
If you want to set a rectangle somewhere you could definitely use an UIView(), If you´re using your storyboard make sure to set the right constraint and if you´re doing it programmatically you could do the following to get it work with all phone sizes:
Swift 3.0:
let screen = UIScreen.main.bounds
let anotherView = UIView(frame: CGRect(x: 0, y: 0, width: screen.width, height: 45))
anotherView.backgroundColor = UIColor.blue
view.addSubview(anotherView)
Swift 2.x:
let screen = UIScreen.mainScreen().bounds
let anotherView = UIView(frame: CGRect(x: 0, y: 0, width: screen.width, height: 45))
anotherView.backgroundColor = UIColor.blueColor()
view.addSubview(anotherView)
Related
I am trying to add a view onto the screen using the following code:-
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let size = CGSize(width: 564.0, height: 783.0)
let host = UIView(frame: CGRect(x: 0.0, y: 0.0, width: size.self.width, height: size.self.height))
self.view.addSubview(host)
let particlesLayer = CAEmitterLayer()
particlesLayer.frame = CGRect(x: 0.0, y: 0.0, width: size.self.width, height: size.self.height)
host.layer.addSublayer(particlesLayer)
host.layer.masksToBounds = true
}
But, the view is not able to cover the entire screen on an iPad, but, is able to cover only the size of an iPhone screen. Could anyone please let me know what can I do to resolve this issue? Thanks a lot for the help!🙏
You can use below to get device's screen size.
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
However, you should consider using autolayout. It is more clean and useful compare to your approach.
I have a container view displaying a UIPageViewController. Everything is working ok, the only problem is the dots that display the pages. They are not able to adjust with the changing screen sizes.
pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 525,width: UIScreen.main.bounds.width,height: 50))
This is the piece of code I am using to change where the dots are displayed mainly the UIScreen.main.bounds.maxY - 525 part. I am just checking on how I can have them adapt to the different screen sizes. Screen shots below of the problem. This first orange screen is correct on where I want the dots to be and the second one is wrong.
More Code
func configurePageControl() {
// The total number of pages that are available is based on how many available colors we have.
pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 525,width: UIScreen.main.bounds.width,height: 50))
self.pageControl.numberOfPages = orderedViewControllers.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.black
self.pageControl.pageIndicatorTintColor = UIColor.white
self.pageControl.currentPageIndicatorTintColor = UIColor.black
self.view.addSubview(pageControl)
}
How I am calling the views
lazy var orderedViewControllers: [UIViewController] = {
return [self.newVc(viewController: "sbBlue"),
self.newVc(viewController: "sbRed")]
}()
Here is solution for your issue.
You can get maxY position of your green or orange view and change your code like as follow. It will set your pageControl's vertical position by removing 30 pixel from bottom of your green or orange view.
pageControl = UIPageControl(frame: CGRect(x: 0, y: yourOrangeView.bounds.maxY - 30, width: UIScreen.main.bounds.width, height: 50))
Or
pageControl = UIPageControl(frame: CGRect(x: 0, y: yourGreenView.bounds.maxY - 30, width: UIScreen.main.bounds.width, height: 50))
I hope this will fix your issue.
There are two problems here.
You should not be creating your own UIPageControl. UIPageViewController has a UIPageControl already; you should just use it.
If you insist on supplying your own (unnecessary) UIPageControl, you need to position it with auto layout so that it takes account of size changes. This code is wrong:
pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 525,width: UIScreen.main.bounds.width,height: 50))
Don't give the page control a frame; give it auto layout constraints.
How can I set a variable header of a TableView (not of a section) programmatically, without having to worry about the placement?
let headerView = UILabel()
headerView.text = x
headerView.textColor = UIColor.blueColor()
headerView.frame = CGRect(x: 0, y: 0, width: headerView.frame.width, height: 44)
tableView.tableHeaderView = headerView
I got this code, but I don't think I can place the header exactly like it is a 'real' header. Anybody knows how to do this?
As Dmitriy said, this is a normal way to do it:
tableView.tableHeaderView = headerView
Position is however defined by the frame you set in it, so if position is what you want different, you have to play with this line of your code:
headerView.frame = CGRect(x: 0, y: 0, width: headerView.frame.width, height: 44)
E.g., if you want the label to be centered, you can for example do it this way:
headerView.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 44)
headerView.textAlignment = .centered
This would span the label across the width of the tableView and the text alignment would place it to center. Of course, for this to work, the tableView frame has to be already calculated at this point (so maybe you would had to put the line with frame calculation in the layoutSubviews method).
I think this tableView.tableHeaderView = headerView - it's normal way to make header in UITableView.
I would like to add a bottom view in all view controllers which will show the progress of Audio or video play.I need Like this
i did in the following
let window = UIApplication.sharedApplication().keyWindow!
let v = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
window.addSubview(v);
v.backgroundColor = UIColor.blackColor()
let v2 = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 50))
v2.backgroundColor = UIColor.whiteColor()
window.addSubview(v2)
But its not displaying in every screen.
I have an approach for you.
Firstly you have to manage this in global way.
You need to create an Object of AppDelegate. And make a function in that class in which you can code
Then on loading your main view, you need to call that function so it will be visible as you want.
I have a UIView that holds a UILabel inside.
After applying affine transform on the UIView using:
myView.transform = CGAffineTransformMakeScale(4, 4);
My UILabel (which is a sub view to myView) grows as well.
Is there a way to prevent this?
i tried:
1) Using the CGAffineTransformIdentity flag on the label.
2) Adding a superview to myView and adding myView as superview's subview, and the label as a subview to the superview (and not myView).
Non of them seem to be working, the label keeps growing.
Any ideas?
You answered your own question with option 2. Not sure why it's not working since you did not supply any code. The playground code below shows it will work. Uncomment out the last line to transform the subview but not the label.
import UIKit
import XCPlayground
let superview = UIView(frame: CGRect(x: 10, y: 10, width: 200, height: 200))
XCPlaygroundPage.currentPage.liveView = superview
superview.backgroundColor = UIColor.redColor()
let view = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
view.backgroundColor = UIColor.greenColor()
superview.addSubview(view)
let label = UILabel(frame: CGRect(x: 20, y: 10, width: 40, height: 40))
label.text = "Hello"
superview.addSubview(label)
//view.transform = CGAffineTransformMakeScale(2, 2)