AutoLayout for programmatically created uiview element - ios

When i try to create an uiview with loading text and activityindicator, even though it looks centered in iphone 6, it is not centered in iphone 5.
How can i use auto layout for this programatically created UIView ?
I am trying to center this uiview in all screen sizes
boxView = UIView(frame: CGRect(x: TableView.frame.midX - 60, y: TableView.frame.midY - 15, width: 180, height: 50))
boxView.backgroundColor = UIColor.blackColor()
boxView.alpha = 0.5
boxView.layer.cornerRadius = 10
var activityView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
activityView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityView.startAnimating()
var textLabel = UILabel(frame: CGRect(x: 60, y: 0, width: 200, height: 50))
textLabel.textColor = UIColor.whiteColor()
textLabel.text = "Loading"
boxView.addSubview(activityView)
boxView.addSubview(textLabel)
TableView.addSubview(boxView)
EDIT: I tried this and it seems working good
var bounds: CGRect = UIScreen.mainScreen().bounds
var width:CGFloat = bounds.size.width
var height:CGFloat = bounds.size.height
boxView = UIView(frame: CGRect(x: width/2 - 90, y: height/2 - 100, width: 180, height: 50))

Related

How to specify which child view does not inherit shadows when setting shadows for the parent view?

I want bView to be free of shadows. How can I achieve this?
I want to shadow all subviews of the superView, so I cannot remove the shadow settings of the superView.
let superView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
superView.layer.shadowColor = UIColor.lightGray.cgColor
superView.layer.shadowOffset = CGSize(width: 0, height: 3)
superView.layer.shadowOpacity = 0.2
superView.layer.shadowRadius = 5
self.view.addSubview(superView)
let aView: UIView = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
aView.backgroundColor = .white
superView.addSubview(aView)
let bView: UIView = UIView(frame: CGRect(x: 10, y: 230, width: 100, height: 100))
bView.backgroundColor = .white
superView.addSubview(bView)
I have tried bView.clipsToBounds = true but it's not working.
If you give superView a non-clear background color then none of its subviews will get a shadow. You can then apply a shadow to the subviews that you want to have a shadow.
let superView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 350))
superView.backgroundColor = .white // <-- Add this line
superView.layer.shadowColor = UIColor.black.cgColor
superView.layer.shadowOffset = CGSize(width: 0, height: 3)
superView.layer.shadowOpacity = 0.2
superView.layer.shadowRadius = 5
view.addSubview(superView)
let aView: UIView = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
aView.backgroundColor = .white
// Add shadow to aView
aView.layer.shadowColor = UIColor.black.cgColor
aView.layer.shadowOffset = CGSize(width: 0, height: 3)
aView.layer.shadowOpacity = 0.2
aView.layer.shadowRadius = 5
superView.addSubview(aView)
let bView: UIView = UIView(frame: CGRect(x: 10, y: 230, width: 100, height: 100))
bView.backgroundColor = .white
superView.addSubview(bView)
If you have several views that need a shadow then I would define a method to make it easier:
extension UIView {
func addShadow() {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 0, height: 3)
layer.shadowOpacity = 0.2
layer.shadowRadius = 5
}
}
Then your code becomes something like this:
let superView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 350))
superView.backgroundColor = .white
superView.addShadow()
view.addSubview(superView)
let aView: UIView = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
aView.backgroundColor = .white
aView.addShadow()
superView.addSubview(aView)
let bView: UIView = UIView(frame: CGRect(x: 10, y: 230, width: 100, height: 100))
bView.backgroundColor = .white
superView.addSubview(bView)

how can I setting background show full screen?

how can I setting background show full screen?
when I used this code, I can set full screen
private let usernameEmailField: UITextField = {
let field = UITextField()
field.placeholder = "email"
field.returnKeyType = .next
field.leftViewMode = .always
field.leftView = UIView(frame: CGRect(x:0, y:0, width: 10, height: 0))
field.autocapitalizationType = .none
field.autocorrectionType = .no
field.layer.masksToBounds = true
field.layer.cornerRadius = Constants.cornerRadius
field.backgroundColor = .secondarySystemBackground
field.layer.borderWidth = 1.0
field.layer.borderColor = UIColor.secondaryLabel.cgColor
return field
}()
private let headerView: UIView = {
let header = UIView()
header.clipsToBounds = true
let backgroundImageView = UIImageView(image: UIImage(named: "camping_background"))
header.addSubview(backgroundImageView)
return header
}()
headerView.frame = CGRect(
x: 0,
y: 0.0,
width: view.width,
height: view.height
)
usernameEmailField.frame = CGRect(
x: 25,
y: headerView.botoom + 10,
width: view.width - 50,
height: 52.0
)
But if I set height: view.height I can not show my textfield. so how do I modify?
headerView.frame = CGRect(
x: 0,
y: 0.0,
width: view.width,
height: view.height
)
usernameEmailField.frame = CGRect(
x: 25,
y: headerView.botoom + 10,
width: view.width - 50,
height: 52.0
)
passwordField.frame = CGRect(
x: 25,
y: usernameEmailField.botoom + 10,
width: view.width - 50,
height: 52.0
)
if I used height: view.height/3.0, if can show textfield, but the background is not fullscreen
I hope I can show like this one, this is my android page.
so, if I don't use the storyboard, how can I design in the my LoginViewController
Take a look at the code here:
headerView.addSubview(backgroundImageView)
...
headerView.botoom + 10,
You are stacking your views and the background vertically!
But
You should set the image behind the textfields (instead of above them)
Here is the how your view hierarchy could be looks like:
- Main View
- Main Content
- Logo image view
- User name text field
- Password text field
- Login button
- Create account button
- Background image view
As you can see, the Background image view is below the other contents
Note that it's not necessary to group content to layout your view but its very useful method for separating components

adding space between icon and text in textfield

I have a text field where i have added a text and icon. Like screenshot below:
However i want to have space between the icon and the text..
I tried this code:
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
let image = UIImage(named: "username")
imageView.image = image
let viewLeft: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 20))
viewLeft.addSubview(imageView)
but got this:
how to add the space after the icon not before?
You can try below code :-
public func setLeftView(of image: UIImage!) {
//setting left image
self.paddingLeft = 50
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let paddingImage = UIImageView()
paddingImage.image = image
paddingImage.contentMode = .scaleAspectFit
paddingImage.frame = CGRect(x: 15, y: 0, width: 23, height: 40)
paddingView.addSubview(paddingImage)
self.leftView = paddingView
self.leftViewMode = UITextFieldViewMode.always
}

how to make cameraOverlayView along the y axis (swift3)

I am making a photo app that I want the imagePicker screen to have blocks of red to pre mask the photo before it crops. My following code gets a roadblock on the top x axis. I would like to place another red box along the entire y axis where the yellow rectangle is.
let blockView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 150))
blockView.backgroundColor = UIColor.red
imagePicker.cameraOverlayView = blockView
Please try the following :
let mainView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height-150))
let blockView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 150))
blockView.backgroundColor = UIColor.red
let blockView1 = UIView.init(frame: CGRect.init(x: 0, y: 170, width: 100, height: self.view.frame.size.height-320))
blockView1.backgroundColor = UIColor.green
mainView.addSubview(blockView)
mainView.addSubview(blockView1)
imagePicker.cameraOverlayView = mainView

How to get correct width for view with content by systemLayoutSizeFittingSize when height gived

I want to calculate view size with autolayout, but it can not get right value. Here is my code
// build views
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let posterView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 100))
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
titleLabel.text = "Title "
titleLabel.font = UIFont.systemFontOfSize(16) // label height = 19.5
view.addSubview(posterView)
view.addSubview(titleLabel)
posterView.snp_makeConstraints { (make) in
make.top.left.right.equalTo(view).offset(0)
make.width.equalTo(posterView.snp_height).multipliedBy(16.0 / 9.0)
}
titleLabel.snp_makeConstraints { (make) in
make.top.equalTo(posterView.snp_bottom).offset(0)
make.left.equalTo(view).offset(0).priority(750)
make.right.equalTo(view).offset(0).priority(750)
make.bottom.equalTo(view.snp_bottom).offset(0)
}
// calculate
view.translatesAutoresizingMaskIntoConstraints = false
let heightConstraint = NSLayoutConstraint(item: view, height: 90+19.5)
view.addConstraint(heightConstraint)
let fittingSize = view.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
// the fittingSize is (40,109.5), not expect value (160, 109.5)
Please don't tell me how to calculate dynamic height for UITableViewCell. This is a opposite problem from height to width.
Thanks

Resources