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 :
- top wall random points( where y is fixed to 0, and varying x):
topRandomPoint = CGPointMake(CGFloat(arc4random_uniform(UInt32(viewWidth))), 0)
- 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))))
- Bottom random points ( where y is fixed to max-height (e.g 568) and x varying)
bottomRandomPoint = CGPointMake(CGFloat(arc4random_uniform(UInt32(viewWidth))), viewHeight)
- Left random points (where x is fixed to 0 and y is varying)
leftRandomPoint = CGPointMake(0, CGFloat(arc4random_uniform(UInt32(viewHeight))))
Related Topics
Query Value Between Two Other Values in Firebase
How to Get Parallax Effect on UIbutton in Tvos
How to Name File Stored to Files App via Uactivityviewcontroller
How to Set .Realm File on Realm
Wkwebview Problems in Macos Mojave
Validatemenuitem or Menuwillopen Not Called for Nsmenu
Macos Remote Push Notifications Not Showing Alert Banner
Create Skspritenode with an Asset Programmatically
How to Set Window Level in Swift
How to Disabled Some Default Functionality in Scene View When Allowscameracontrol = True
Disable Bringing App Window to Front. After Closing Another Window
Swift-Making an Sknode Simply "Move Forward" on an Angle
Use Tableviewcontroller Inside Skscene
Uibutton State Changing While Scrolling The Tableview with Multiple Sections - Swift