Change position of a IBOutlet in Swift - button

So i have this button:
#IBOutlet var bouton: UIBarButtonItem!
And i want to change is position
But the buton doesn't seems to have a position property like for SKSpriteNode
So is there a way to change is position ?

If you want to programmatically change the position of any view, you can use this:
button.frame.origin = CGPoint(x: 10, y: 10)

You can't set the button with myButton.frame.origin if you are using autolayout. So you must add constraints. For example:
let buttonConstraintY = NSLayoutConstraint(item: youButton, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 10)
self.view.addConstraint(buttonConstraintY)

Related

Why doesn't my custom component update when I change properties with setAttribute?

I think I don't quite understand how components and entities work.
I'm using the box component that appears in the a-frame help (https://aframe.io/docs/1.3.0/introduction/writing-a-component.html#example-box-component), and I want to modify a property. When doing so, I thought that the update would be activated but it does nothing. In fact the oldData is always empty and never enters in the update option (except for the first time):
<script>
AFRAME.registerComponent("box", {
...
}
</script>
<a-scene> </a-scene>
<script>
const gScene = document.querySelector("a-scene");
var camara = document.querySelector("[camera]");
camara.setAttribute("position", { x: 0, y: 0, z: 10 });
var p00 = document.createElement("a-entity");
p00.setAttribute("box", { width: 1, height: 1, depth: 1, color: "#f00" });
gScene.appendChild(p00);
p00.setAttribute("height", 3);
p00.setAttribute("color", "#00f");
</script>
First part of the code plot a red box with 1x1x1 dimension. But when I change the height or color attribute, don't do anything. What I have to do to fire the update?
Thanks in advance
You need to update the box attribute - setAttribute("height", XXX) will trigger an update in the height component.
If color is in the box schema, then:
// this updates the color property of the box attribute,
// triggering "update" in the "box" component
p00.setAttribute("box", "color", "red")
// this update the height attribute,
// triggering "update" in the "height" component
p00.setAttribute("height", "10");

UIBarButtonItem not change image automatically when switching dark mode/light mode

I am trying to integrate dark mode support to my app. But when setting image icon for UIBarButtonItem, it seem only works when the first time UIBarButtonItem is shown, it is not changed when I switch between dark mode/light mode.
When using that image with other UIButton, it works fine.
So I wonder if I am missing something?
P/s: I have to use that trick for updating image:
let item: UIBarButtonItem = UIBarButtonItem()
let button: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 26, height: 19))
button.setImage(UIImage(named: "hamburger"), for: .normal)
button.addTarget(self, action: action, for: .touchUpInside)
item.customView = button
Seems to be a bug in iOS, but one way around this is, to use traitCollection delegate method to set the correct image manually.
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
let imageAsset = UIImage(named: "YOUR_IMAGE_NAME")?.imageAsset
let resolvedImage = imageAsset?.image(with: traitCollection)
self.YOUR_BUTTON.image = resolvedImage
}

dynamic create checkbox as array size in swift

I Want to create a view with dynamic create checkbox i know how to create c checkbox dynamic but how to manage size of view?
Here is my Code:
var spacer: CGFloat = 23
for comments in fixer as NSArray {
var counter = ""
print(comments)
counter = comments as! String
let button = UIButton(frame:CGRect(x: 8, y: 0+spacer , width: 20, height: 20))
button.setTitle("hi", for: .normal)
self.assignfixerView.addSubview(button)
let label = UILabel(frame:CGRect(x: 40, y: 0+spacer , width: 80, height: 22))
label.text = counter
self.assignfixerView.addSubview(label)
spacer = spacer + 23
}
Here is my view :
in this user are not fix and i want status(label) and textfield position change and if i create this button than how's the selection process?
Please give me better /suggestion if any better controller than it will be ok
OR any dropdown with multiple selection and after selection all will be display in textfield
Thank You in Advanced!

How to setup a constraint to a tvOS focused element

I have a UICollectionView in my tvOS app that has a image and a caption text. I've setup a constraint to pin the caption text just under the image.
When the cell is focused and the images grows, the text does not move and stays over the image. Is there a way to set a constraint that takes in account the focused image size?
You can use a UIImageView property focusedFrameGuide for that. It returns a UILayoutGuide object, which unfortunately can't be used in Interface Builder, but you can create constraints with it in code. Note that this constraint makes sense only when the view is focused, so you have to set its active property according to the self.focused property.
First create the constraint on view initialization:
self.focusedSpacingConstraint = NSLayoutConstraint(item: imageView.focusedFrameGuide, attribute: .BottomMargin, relatedBy: .Equal, toItem: label, attribute: .Top, multiplier: 1, constant: 0)
//add it to the view and set active to false
Then activate either this constraint or the default one depending on the focus:
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
coordinator.addCoordinatedAnimations({
self.focusedSpacingConstraint.active = self.focused
self.spacingConstraint.active = !self.focused
//set label's transform and animate layout changes
}
You can also use the focusedFrameGuide to set label's height (as a percentage of image's height).
The advantage of this approach is that you don't have to change the (hardcoded) constant whenever the image size changes.
I ended up setting a #IBOutlet to my spacing constraint and I update it in the didUpdateFocusInContext method. To add some effect, I also transform the label.
In my collection cell:
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
coordinator.addCoordinatedAnimations({
if self.focused {
self.spacingConstraint.constant = 30
UIView.animateWithDuration(0.3) {
self.label.transform = CGAffineTransformMakeScale(1.15, 1.15)
self.layoutIfNeeded()
}
}
else {
self.spacingConstraint.constant = 0
UIView.animateWithDuration(0.3) {
self.label.transform = CGAffineTransformMakeScale(1, 1)
self.layoutIfNeeded()
}
}
}, completion: nil)
}
Use this code, I hope it help you. This code write into the custom cell class.
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
if (self.focused) {
self.custom_ImageView.adjustsImageWhenAncestorFocused = true
self.custom_LabelView1.frame = CGRectMake(0, 279, 548, 29)
}
else {
self.custom_ImageView.adjustsImageWhenAncestorFocused = false
self.custom_LabelView1.frame = CGRectMake(0, 259, 548, 29)
}
}

UIPopoverController displays at wrong position

I want to show a UIPopoverController when clicking on a label. All elements are laid out with AutoLayout.
// In TorHeimPopoverViewController.swift
// self.view is TorHeimPopoverViewController's view
popover.presentPopoverFromRect(label.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Left, animated: true)
It's displayed in another UIPopoverController (TorHeimPopoverViewController) and there in a Container
MainAppView -> TorHeimPopoverViewController -> ContainerView
How can I fix this?
The problem is, the inView parameter is wrong for this purpose. The rect of label.frame is in relation to the ContainerView, in which it is.
For example, when label.frame is x = 300, y = 100, it will take these coordinates, but use self.view as origin and not the container.

Resources