How to get Parallax Effect on UIButton in tvOS? - swift

I'm trying to make some UIButtons in a UIView for a tvOS app but have been unable to get them to display the new parallax effect in the simulator. I successfully set up a TV image stack in images.xcassets called "startReadingButton" and my buttons load with the file, but they do not display the shiny parallax effect when swiping around on the remote in the simulator. Here is how I am loading my UIButtons:
for button in 1...5 {
let image = UIImage(named: "startReadingButton")
let newButton = UIButton(type: .Custom)
newButton.frame = buttonRects[button - 1]
newButton.setBackgroundImage(image, forState: UIControlState.Focused)
newButton.imageView?.image = image
newButton.imageView?.adjustsImageWhenAncestorFocused = true
newButton.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.PrimaryActionTriggered)
newButton.tag = button
newButton.canBecomeFocused()
newButton.adjustsImageWhenHighlighted = true
newButton.clipsToBounds = false
self.addSubview(newButton)
self.homeButtons.append(newButton)
}
So far, I have tried almost every variation I could think of to find a solution, like setting the button type to both .Custom and .System, setting the image in different parameters of the UIButton, etc. However I cannot get the buttons to enter parallax mode and move around when they are focused.
Anyone know what I need to do to get the desired parallax effect?

Solved my problem -
Set your LSR or Apple TV image stack in your UIButton's setImage property for the UIControlState.Focused state.
Set the imageView?.clipsToBounds = false on the UIButton.
Set newButton.imageView?.adjustsImageWhenAncestorFocused = true on the UIButton.
I have got the following code to work as expected:
for button in 1...3 {
let buttonUnpressedTexture = UIImage(contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("startReadingUnpressed.png"))!
let buttonPressedTexture = UIImage(contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("startReadingPressed.png"))!
let newButton = UIButton(type: .Custom)
newButton.frame = buttonRects[button - 1]
newButton.setImage(buttonUnpressedTexture, forState: UIControlState.Normal)
newButton.setImage(buttonPressedTexture, forState: UIControlState.Highlighted)
newButton.setImage(UIImage(named: "StartReadingButton"), forState: UIControlState.Focused)
newButton.imageView?.clipsToBounds = false
newButton.imageView?.adjustsImageWhenAncestorFocused = true
newButton.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.PrimaryActionTriggered)
newButton.tag = button
newButton.canBecomeFocused()
self.addSubview(newButton)
}
"StartReadingButton" is an Apple TV image stack in my images.xcassets catalog.

Related

How can I add two images (one at the left and one on the right side) in a button in Xcode programaticly? I need also a title of a button

func setupButtonUI() {
if let detailsImage = UIImage(named: "detalji_icon") {
setImage(detailsImage, for: .normal)
contentHorizontalAlignment = .left
contentVerticalAlignment = .center
}
if let arrowImage = UIImage(named: "posalji_zahtev_black_icon") {
setImage(arrowImage, for: .normal)
contentHorizontalAlignment = .right
contentVerticalAlignment = .center
}
}/this is one of the things I tried, but whit this I get only the second image and its set in the center not right and the left image is hot even there/
UIButton has only one imageView, you can create custom button like in this answer
https://stackoverflow.com/a/43112735/13628690
Found the next solution: Made a custom UIView and added two images and label as a subview of customView. Did the constraints programaticly. Just added UITapGestureRecognizer (addGestureRecognizer) at the entire view and its all elements are clickable. The design and functionality are satisfied.

How can I change my custom cell background when pressing a button?

I have a CollectionView and a Button inside of an UIView. The purpose of the button is change the theme of the app, and I can change all colors, except for the backgroundColor of a nameContainerView on my TatodexCell (a custom cell).
I can't figure out how to access that property, and then, change the color I want.
THE FOLLOWING CODE IS LOCATED ON MY TatodexController FILE
My button property is:
let buttonChangeTheme: UIButton? = {
let button = UIButton()
button.setTitle("Change to blue theme", for: .normal)
button.addTarget(self, action: #selector(themeButtonClicked), for: .touchUpInside)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.layer.borderWidth = 3
button.layer.borderColor = Colors.mainBlack?.cgColor
button.backgroundColor = Colors.darkBlue
button.tintColor = Colors.mainWhite
return button
}()
My button-func is this one:
#objc func themeButtonClicked() {
clickCheck = !clickCheck
if clickCheck {
navigationController?.navigationBar.barTintColor = Colors.lightBlue
collectionViewPokemon?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.backgroundColor = Colors.darkRed
buttonChangeTheme?.setTitle("Return to red theme", for: .normal)
}
else {
navigationController?.navigationBar.barTintColor = Colors.lightRed
collectionViewPokemon?.backgroundColor = Colors.darkRed
buttonChangeTheme?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.setTitle("Change to blue theme", for: .normal)
}
}
THE FOLLOWING CODE IS LOCATED ON MY TatodexCell FILE
The nameContainerView I wanna modify it's bg color is:
lazy var nameContainerView: UIView = {
let nameView = UIView()
nameView.backgroundColor = Colors.lightRed
nameView.addSubview(nameLabel)
nameLabel.center(inView: nameView)
return nameView
}()
My goal is to change nameView.backgroundColor = Colors.lightRed to nameView.backgroundColor = Colors.lightBlue, but I can't access that property.
I would really appreciate any help or advice. Perhaps the solution is hiding in plain sight, but I've tried many ways and none of them worked. Let me know if another chunk of code is needed to be shown.
If you implement the tintColorDidChange() method in your custom collection view cell, and in that method, you set the cell's background color to the tint color, then when you change the owning UICollectionView's tint color, the cells's tintColorDidChange() methods will fire and you'll see the change.

Swift: Button with image bigger than button

I can't figure out a way to make a button look like in the following image.
The icon is supposed to be bigger than the button.
I already tired this, but it doesn't work:
let image = UIImage(named: "CircleForButtonCountries")
goToCoutriesButtonMain.imageView?.contentMode = UIViewContentMode.scaleAspectFit
goToCoutriesButtonMain.setImage(image, for: UIControlState.normal)
Try this code
let image = UIImage(named: "CircleForButtonCountries")
goToCoutriesButtonMain.imageView?.contentMode =
UIViewContentMode.scaleAspectFit
goToCoutriesButtonMain.setBackgroundImage(image, for: UIControlState.normal) // use setBackgroundImage instead of setImage
Working in Swift 5:
yourButton.clipsToBounds = true
yourButton.contentMode = .scaleAspectFill
yourButton.setBackgroundImage(UIImage(named: "yourImage"), for: .normal)

Touch image in navigation bar for redirecting another view

I didnt see the image in navigation bar so how can i give event touch inside for that image ? Need help !
This is my code . Remember the image have not in the view.
Detail more for my issue is notification(the bell)
Try and see if this helps.
let img = UIImage(named: "icon.png");
let btn = UIButton(frame: CGRectMake(0,0,(statImg?.size.width)!,(statImg?.size.height)!));
btn.setBackgroundImage(statImg, forState: .Normal);
btn.addTarget(self, action: #selector(YourController.yourMethod), forControlEvents: .TouchUpInside);
let barBtn: UIBarButtonItem = UIBarButtonItem.init(customView: btn);
navigationItem.rightBarButtonItem = barBtn;

How to prevent side area clickable of UIBarButtonItem?

`
Whenever i click on this area , action also perform on this.
Don't worry about this behavior, iOS auto tapp near element if there is not other element.
If you really want to prevent to click except button, then you have to put Element on unused area which should be inherited from UIControl or able to get UserInteraction.
I got solution. I put one UIButton between these two UIBarButtonItem and make it custom and nil it's selector.
Also, you can try this: (Swift version)
let btnName = UIButton()
btnName.setImage(UIImage(named: "settings_filled_25"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 30, 30)
btnName.addTarget(self, action: Selector("toggleRight"), forControlEvents: .TouchUpInside)
var rightView = UIView()
rightView.frame = CGRectMake(0, 0, 30, 30)
rightView.addSubview(btnName)
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = rightView
self.navigationItem.rightBarButtonItem = rightBarButton