Reduce height of a UIView from the top - ios

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)

Related

Wrong position UIScrollView content with statusbar

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

why is content in UICollectionViewCell located incorrectly?

I have next code in controller, which set frame of UICollectionViewCell:
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: self.view.frame.size.width / 3.8, height: self.view.frame.size.width / 3.8)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.scrollDirection = .vertical
devicesCollectionView.collectionViewLayout = layout
Chunk of code in delegate method, which call cell's method, which set data in it:
if let deviceCell = cell as? DevicesCollectionViewCell , let devices = room?.devices, let roomName = room?.roomName {
deviceCell.setDeviceInfoInCell(device: devices[indexPath.item], roomName: roomName, width: self.view.frame.size.width / 3.8)
}
chunk of code in cell's class:
self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: width, height: width)
self.stateImageView.frame = CGRect(x: self.frame.origin.x + width - (width / 3), y: self.frame.origin.y, width: width / 3, height: width / 3)
self.lightImageView.frame = CGRect(x: self.frame.origin.x + 10, y: self.frame.origin.y + 10, width: width / 3, height: width / 3)
self.sensImageView.frame = CGRect(x: self.frame.origin.x + 10, y: self.frame.origin.y + 10, width: width / 5, height: width / 5)
self.humdityImageView.frame = CGRect(x: self.frame.origin.x + 10, y: self.frame.origin.y + sensImageView.frame.size.height + 15, width: width / 5, height: width / 5)
But when my CollectionView is showed, imageviews inside placed incorrectly, sometimes I didn't see them. Why they gets incorrect coordinates?
Screenshots: only one picture in one cell
and here icons goes anywhere:
Updating
according the comments I'm trying to use layoutIfNeeds and adding this method to my cell's class:
override func layoutSubviews() {
self.layoutIfNeeded()
self.stateImageView.layoutIfNeeded()
self.sensImageView.layoutIfNeeded()
self.humdityImageView.layoutIfNeeded()
self.lightImageView.layoutSubviews()
}
But it is don't solved my problem, and now labels goes anywhere too. Please help me. New screenShot:

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)

Updating to latest Swift version problems

I just updated to the latest version of Swift and I am getting issues with a breakpoint error. There are no errors in the console.
backgroundsetting.innerColor = UIColor.rgb(fromHex: 0xB0E7D7)
backgroundsetting.outterColor = UIColor.rgb(fromHex: 0x005E7D)
backgroundsetting.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height))
self.view.addSubview(backgroundsetting)
Then after the update it wanted me to set the backgroundsetting.frame as! CGRect and it crashes the app when I open that part of the app. Why is this occurring? Here is the after code:
backgroundsetting.innerColor = UIColor.rgb(fromHex: 0xB0E7D7)
backgroundsetting.outterColor = UIColor.rgb(fromHex: 0x005E7D)
backgroundsetting.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height)) as! CGRect
self.view.addSubview(backgroundsetting)
The backgroundsetting.frame property is a type of CGRect, why not creating a CGRect like this:
backgroundsetting.frame = CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height)
If you are using constraint for your view and then use height:self.view.frame.size.height and width:self.view.frame.size.width , it will not show proper height and width as compare to your iPhone screen.
For that its better to use UIScreen size i.e UIScreen.main.bounds.height and UIScreen.main.bounds.width , it will always show original width and height of your iPhone.
backgroundsetting.frame = CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:UIScreen.main.bounds.width, height: UIScreen.main.bounds.height) as! CGRect
self.view.addSubview(backgroundsetting)

Setting UICollectionView frame at runtime

I am trying to set the frame of my UICollectionView at runtime.
I tried using
mainCollectionView.frame = CGRect(x: 20, y: 20, width: self.view.frame.width-20, height: self.view.frame.width-20)
in my viewDidLoad but unfortunately it will always stay as in IB.
I am not yet using constraints.
Otherwise resizing my cells is working in cellForItem:
collectionCell.frame.size.width = self.view.frame.width/8 * 3
Try to set frame in viewDidLayoutSubviews
you are wrong write height because you are set width at height.
try this line without navigation bar:
mainCollectionView.frame = CGRect(x: 20, y: 20, width: UIScreen.mainScreen().bounds.size.width - (20 * 2), height: UIScreen.mainScreen().bounds.size.height - (20 * 2))
Run the following in one of the LayoutSubviews overrides. This will tell collection view to update its frame.
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
mainCollectionView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
mainCollectionView.collectionViewLayout.invalidateLayout()
}

Resources