UIView added to SKScene have strange border - uiview

I need to add UIView with white background (have HUD class that is used through out the app) to the SKScene with white background. The code is something like this:
class CategoriesGameScene: SKScene {
override func didMove(to view: SKView) {
let view = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 50))
view.backroundColor = UIColor.white
}
}
Everything works, however there is a problem - on the border of the UIView there is small black/greyish border (so two views are not "blending") with each other. The funny thing is when I am trying to make a screenshot (so I can post an example here) - there is no border on the screenshot and everything looks as it should and also if i am tapping homebutton to multitasking view on the iphone line is also disappearing. Is there a proper way to add UIView to SKScene or can I try something different to get rid of that?

You can resolve this issue by checking "Allow Transparency" option of SKView properties in Storyboard.

Related

why Button SubView will not go to the back in swift 4?

I have a view class with xib file so I want to use this view at the background of the button so I used subview but the problem is that the subview of the button will be inFront of the button so I can't click button any more I used UI Debugging mode and I realized that the button is at the back of the view so I used send to back in my codes But still the view is inFront of the Button !
let adsView = videoAdsView()
func adsOutlet() {
self.adsView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
self.showAdsOutlet.addSubview(adsView)
self.showAdsOutlet.sendSubview(toBack: self.adsView)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
adsOutlet()
}
put following code at the end of adsOutlet method:
self.adsView.isUserInteractionEnabled = false
If you want to add your button as a subview of your background view then you can just do that, and your button will be in front of the background:
self.backgroundView.addSubview(adsButton)
If you don't want adsButton as a subview of the backgroundView but you want to make sure it's presented in front of the backgroundView then assuming that both are subview's of the parent view do this:
self.view.sendSubview(toFront:adsButton)
Please try this code:
Make the button background color clear.
Then write this code:
self.view.bringSubview(toFront: showAdsOutlet)
It may helps to you. Thank you

Tap event for a UIView with alpha set going to underlying button

When loading data from server, we mask our UI using a UIView like the following sample code:
let loadingView = UIView()
loadingView.tag = 9999
loadingView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
loadingView.backgroundColor = UIColor.white.withAlphaComponent(0.4);
controller.view.addSubview(loadingView)
As you can see this loadingView takes the entire width and height of the controller and practically masks the screen. Having alpha means underlying content is translucently visible. We remove this view from controller after data has loaded like this:
for view in controller.view.subviews
{
if view.tag == 9999
{
view.removeFromSuperview()
}
}
However we are seeing strange issue where-in if a user keeps tapping a UIButton on the controller while the loadingView is shown, tap on the button is invoked as many times after the loadingView has been removed from the controller. In other words, our action for UIButton tap gets invoked as many times as you tapped it while the loadingView was shown.
Are we doing something wrong? How can we prevent the tap on UIButton from being invoked while the loadingView is shown?
You should doable user interaction on the loading view.
loadingViee.userInteractionEnabled = false
As you said the loading view is on all over the screen.
You can Ignore touch of your screen during the loading view Once finished loading view you can continue Interaction as like below.
UIApplication.shared.beginIgnoringInteractionEvents()
UIApplication.shared.endIgnoringInteractionEvents()

How do I add a UISlider to a SKScene that isn't the main scene of the View Controller

I'm creating a Sprite Kit game with one view controller and multiple skscenes. Basically, the application loads the view controller, which loads the menu scene, and then I use SKTransitions to go between scenes. In creating a settings scene, I would like to be able to use certain UIKit elements like a UISlider - however, I can only ever get the UISlider to show up on the menu scene. What exactly do I need to do in order to get the UISlider to show up only on the settings scene? Thanks for taking the time to read this.
As Knight of dragon said its best to avoid using UIKit in SpriteKit as much as possible.
However in some cases UIKit is very useful in SpriteKit. For example I use UICollectionViews to show my World and Level select menu in one of my games because its the nicest option (cell reuse, dequeueing, animation effects etc).
Just use the view property of a SKScene to add UIKit elements in a specific scene.
class MenuScene: SKScene {
let slider = UISlider(frame: CGRect(x: 250, y: 250, width: 280, height: 20))
override func didMoveToView(view: SKView) {
loadSlider()
}
private func loadSlider() {
...
view?.addSubview(slider) // if you are calling view outside of DidMoveToView it is an optional, so use ?
}
}
Dont forget to remove it again when you change scene as its added to your GameViewController.
override func willMoveFromView(view: SKView) {
slider.removeFromSuperview()
}
Hope this helps.

Swift : addSubview before StoryBoard

Im not new to swift but new to using storyboard, i typically do everything programmatically but decided to change it up a bit.
I have a storyboard with a bunch of UIButtons(numbers 0 - 9, similar to a calculator) and i would like to add a view on the lowest layer before all the buttons are added. So in terms of layers : UIView -> UIButtons
I thought simply adding the viewDidLoad() and putting the view first would work but it didn't:
override func viewDidLoad() {
blurView = BlurViewEffect(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
self.view.addSubview(blurView)
designButtons()
}
Any idea how I can load the storyboard last?
There's no way to load the storyboard before the viewDidLoad method. Storyboards get serialized, so basically they're loaded with the init:coder method.
However, typical layouting tools should do the trick for your needs. Simply use view.sendSubviewToBack(blurView) to make sure your blurView is at the back of the view.

How to fix the UIImage overlapping in UIAlertController?

I'm using this code:
// Declare a UIImageView, set its frame and add an image to it
var myImageView = UIImageView(frame: CGRectMake(0, 0, 100, 100))
myImageView.image = UIImageView(image:UIImage(named:"myImage"))
// Let's add the UIImageView to the alertController
alertController.view.addSubview(myImageView)
However, the view is overlapping the UIAlertController:
How can I fix it?
You've no business adding subview to an alert view. It is easy to make a view-controlled presented view that behaves like an alert view, and that's what you should do. That way, you are in charge of the view and you won't have any problems laying it out in a nib / storyboard with autolayout or whatever.

Resources