Wrong position UIScrollView content with statusbar - ios

I'm trying to position UIScrollView at the top of screen to display a popup like Facebook Messenger
The Subview of UIScrollview is under the StatusBar.
How to position it correctly?
Issue:
If I place UIScrollview under the StatusBar, it works
let StatusbarHeight = UIApplication.shared.statusBarFrame.height
super.init(frame: CGRect(x: 0, y: 0, width: Util.getScreenSize().width, height: StatusbarHeight + 50))
self.contentSize = CGSize(width: self.frame.width, height: self.frame.height * 2)
let sub = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height))
self.addSubview(sub)
I expect that the subview is at the top of UIScrollView
Like this.

You might want to try this on the UIScrollView()
self.contentInsetAdjustmentBehaviour = .never

Related

Center UIImageView inside UITableView Header

I have added a UIImageView to my UITableView Header and now I am trying to center is, but it won't center, it goes to the right.
let imageHeader = UIImageView(frame: CGRect(x: 0, y: 0, width: self.tableView.frame.width, height: 50))
let header = UIView(frame : CGRect(x : 0, y : 0, width : self.tableView.frame.width, height : 200))
imageHeader.image = UIImage(named: "paindown-logo.png")
imageHeader.contentMode = .scaleAspectFit
imageHeader.center = CGPoint(x: header.bounds.midX, y: header.bounds.midY);
header.addSubview(imageHeader)
self.tableView.tableHeaderView = header
What am I doing wrong? Why will not center?
reload your imageHeader view then reload your table view after open menu.

Reduce height of a UIView from the top

I am simply trying to set frame of a UIView, but when you try to reduce the height of view, it will be reduced from the bottom of the view, not the top. How can I change hight of view from the top of it not bottom ?
customView = CGRect(x: 0, y:0 , width: self.view.frame.width , height: self.view.frame.height - reduceHeightSize)
You can update frame.origin.y as much as the height.
if reduceHeightSize is positive;
customView = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y + reduceHeightSize , width: self.view.frame.width , height: self.view.frame.height - reduceHeightSize)
if you store it negative reduceHeightSize;
customView = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y - reduceHeightSize , width: self.view.frame.width , height: self.view.frame.height + reduceHeightSize)
It would be better to just keep amount of the size not the direction.
You need to do like...
customView = CGRect(x: 0, y:reduceHeightSize , width: self.view.frame.width , height: self.view.frame.height - reduceHeightSize)
customView = CGRect(x: 0, y:0 , width: self.view.frame.width , height: self.view.frame.height - reduceHeightSize)
The problem of your code is that you change Height in CGRect. and you want to change top position of view so you need to change position of also y of your view so finally your code will be
customView = CGRect(x: 0, y: reduceHeightSize , width: self.view.frame.width , height: self.view.frame.height - reduceHeightSize)
If I understand your question correctly, that's probably because your view is not constrained/anchored at the bottom and thus defaulting to reduce on the top. Try adding a bottom constraint.
I just figured it out what I need:
customView.frame = CGRect(x: 0, y: self.view.frame.origin.y - reduceHeightSize , width: self.view.frame.width , height: self.view.frame.height + reduceHeightSize)

Why doesn't view change when I change bounds?

If I change a UIView's bounds from...
v2.bounds = CGRect(0, 0, 70, 70)
to...
v2.bounds = CGRect(-2000, 0, 70, 70)
... nothing happens - the dimensions stay the same upon rendering. Why is this?
To help understand what bounds does, run this sample code in a view controller's viewDidLoad method:
let newView = UIView(frame: CGRect(x: 50, y: 100, width: 30, height: 30))
newView.backgroundColor = UIColor.blue
let secondView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
secondView.backgroundColor = UIColor.red
newView.addSubview(secondView)
view.addSubview(newView)
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
newView.bounds = CGRect(x: 10, y: 0, width: 30, height: 30)
}
Here we're moving the bounds to the right by 10 points, so you'll see the "second view" (which is red) move to the left by 10 points.
Changing the origin of the bounds changes the coordinate system of the view, which affects the location of all of it's subviews. It doesn't affect its origin with respect to its super view, however. For that, you should change the origin of the frame.
Bounds only takes into account width and height, you are only changing the origin in your example, only changing x to be precise. To accomplish this use frame property:
v2.frame = CGRect(-2000, 0, 70, 70)

Centering a button in UIScrollView

For the life of me, I can't figure out how to center a UIButton in UIScrollView...
let sv = UIScrollView(frame: CGRect(x: 0, y: 0, width: 300, height: 600))
let b = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
sv.addSubview(b)
b.center = sv.center
No matter what I do, the button seems to be off center. This logic works in normal UIView. Why doesn't it with UIScrollView?
you can also use
let b = UIButton(frame: CGRect(x: sv.frame.size.width/2 -15, y: sv.frame.size.height/2 -15, width: 30, height: 30))
for your code first check value of sv.center, if it is OK and your UI is not updating then call layoutIfneeded
Reference : How is layoutIfNeeded used?

AutoLayout for programmatically created uiview element

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))

Resources