How to Use Frame to Set X Position in Loop on Swift

How to use frame to set x position in loop on Swift

You can use stackview as suggested by @matt. If you don't want to use stackview you can calculate the x position by array count and label index like this

let array = ["some1", "some2", "some3"]
func setLabel(){
var i:CGFloat = 0
for text in array{
i += 1
let label = UILabel()
label.backgroundColor = .red
label.frame = CGRect(x: (view.bounds.width/CGFloat(array.count+1))*i-25, y: 100, width: 50 , height: 20)
label.text = text
view.addSubview(label)
}
}

How to loop Multiple UIButton in swift

Just cast i to CGFloat.

self.loopButtonTwo.frame = CGRect(x: CGFloat(i), y: minimalYLoopButton, width: widthLoopButton, height: 70)

Position of a view with .zero frame

Call setNeedsLayout() AND layoutIfNeeded() on the parent view:

let myView = UIView(frame: .zero)
myView.backgroundColor = .red
myView.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(myView)
myView.topAnchor.constraintEqualToSystemSpacingBelow(view.topAnchor, multiplier: 1).isActive = true
myView.leftAnchor.constraintEqualToSystemSpacingAfter(view.leftAnchor, multiplier: 1).isActive = true
myView.widthAnchor.constraint(equalToConstant: 50).isActive = true
myView.heightAnchor.constraint(equalToConstant: 50).isActive = true

view.setNeedsLayout()
view.layoutIfNeeded()
print(myView.frame)

Swift position and constraints programmatically

You can try

self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
button.topAnchor.constraint(equalTo: self.balanceLabel.bottomAnchor,constant:30),
button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
])

Same for the label

Note: Since the button and the label have an intrinsic content size , then we don't have to set width && height

Update position of UIImageView Swift

These values are just thrown away when you set monkeyx and monkeyy below.

var monkeyx = monkey.frame.origin.x
var monkeyy = monkey.frame.origin.y

Assigning the values below just assigns the values from one local variable to another. It does not actually change the coordinates for monkey.

monkeyx = bananax
monkeyy = bananay

You must set the new coordinates on monkey by constructing a new CGRect and assigning monkey's frame:

monkey.frame = CGRect(x: bananax, y: bananay, width: monkey.frame.size.width, height: monkey.frame.size.height)

As you are just setting monkey's origin to banana's origin, you can remove all the var declarations and reduce this to one line by constructing monkey's new frame with monkey's size and banana's origin:

monkey.frame = CGRect(origin: banana.frame.origin, size: monkey.frame.size)

How to get the X and Y of the edges of the screen in Swift

If I got you correctly, what you want is random points which fall in the edges of the view. That means on the four sides of the view, either x or y is fixed. So here is the solution :

  1. top wall random points( where y is fixed to 0, and varying x):

topRandomPoint = CGPointMake(CGFloat(arc4random_uniform(UInt32(viewWidth))), 0)


  1. right side wall points ( where x is fixed to max-width (e.g 320), and varying y)

rightRandomPoint = CGPointMake(viewWidth, CGFloat(arc4random_uniform(UInt32(viewHeight))))


  1. Bottom random points ( where y is fixed to max-height (e.g 568) and x varying)

bottomRandomPoint = CGPointMake(CGFloat(arc4random_uniform(UInt32(viewWidth))), viewHeight)


  1. Left random points (where x is fixed to 0 and y is varying)

leftRandomPoint = CGPointMake(0, CGFloat(arc4random_uniform(UInt32(viewHeight))))



Related Topics



Leave a reply



Submit