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

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

Related

Swift: Adding floating button to TableViewController?

I'm having trouble at TableViewController.
I want to add floating button, but I found out that if I create tableview with TableviewController in Storyboard, then tableview is superview in that view controller, which means only way to add button is adding button in tableview as one of a cell, which is not floating button. (Maybe I'm wrong. I'm a bit confused. I can't add another view by Storyboard.)
I googled several times and I think the only solution is to add button by using UIWindow, but part of the solution codes are deprecated.
I hope I can get alternate solution for my problem.
Obviously the best solution is using UIViewController and adding UITableView and your button as subviews (as #Surjeet Singh suggested in comment). However if you face troubles doing this (maybe too complex right now), you can add UIButton as subview of your keyWindow as workaround. however keep in mind that you need to manually remove the button from keyWindow once your UITableViewController is going to disappear, or else your button will be appearing on other UIViewControllers. Here is the workaround solution:
func addFloatingButton() {
let keyWindow = UIApplication.shared.keyWindow
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
button.backgroundColor = .red
keyWindow?.addSubview(button)
}

My ViewController WITH Large Navigation Bar Scrollers up automatically After loading WKWebView

In my app, I am loading about the page in a view controller with a WebKit WebView, and Viewcontroller has a Large Navigation Controller. At time of view DidLoad Large navigation bar is shown fine.
At this point when I start loading my WebView with a local HTML file.
After loading this WebView, without any user interaction, ViewController automatically scrolls up such that navigation bar shrink to actual small-sized form.
Yes this is the default behaviour of navigation bar if you want to set large navigation bar all the time then you have to go with custom navigation bar. Create your own Navigation bar. Thanks
What will help you is the use of viewLayoutMarginsDidChange() .Using this you can check in everyview if the view size has changed and if so you can set your original navigation title size here , So instead of what i have added , parse your Navigation bar frame values below.
var didChange = false
override func viewLayoutMarginsDidChange() {
if didChange {
print("Height : - \
(self.navigationController?.navigationBar.frame.size.height)")
// set NavigationBar Height here
self.navigationController!.navigationBar.frame = CGRect(x: 0, y: 0, width:
UIScreen.main.bounds.width, height: 96.0)
didChange.toggle()
}
}
Try getting the scroll events of wkWebView.srollView in scrollViewDidScroll , see if something make it scroll.
Or , disable WKWebView.scrollView.scrollEnabled when loading the WebView and enable it back when load completes
Change the contentInsetAdjustmentBehavior property of the ScrollView of WebView to .never in your viewDidLoad method.
webView.scrollView.contentInsetAdjustmentBehavior = .never
You can try :
webView.scrollView.contentInsetAdjustmentBehaviour = .never

UIView added to SKScene have strange border

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.

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()

Animate UITextField

I need to make it so that when I click a button, the UITextField transforms left onto the view from outside the view. However, when I execute the following code, the UITextField starts off in the middle of the viewcontroller, and then when the button is clicked it transforms left onto the view from outside the view. How can I make it so that when the view loads initially, it is not seen until the button is clicked, using swift.
#IBAction func joinCircleButton(sender: AnyObject) {
let button = sender as UIButton
joinTextField.frame.origin.x=500
joinTextField.frame.origin.y=100
if (button.frame.origin.x - 75>0){
UIView.animateWithDuration(0.5, animations:{
button.frame = CGRectMake(button.frame.origin.x - 125, button.frame.origin.y,button.frame.size.width, button.frame.size.height)
button.transform = CGAffineTransformMakeScale(0.5, 0.5);
self.joinTextField.frame=CGRectMake(self.joinTextField.frame.origin.x - 325, self.joinTextField.frame.origin.y,self.joinTextField.frame.size.width, self.joinTextField.frame.size.height)
})
}
}
You need to put the UITextField, or any object that you wish to hide, outside of the view by using a function called:
func viewDidLayoutSubviews()
This function is called just before the screen is loaded. It gets the sub-views ready but doesn't show them on the screen yet. So this is an opportunity to hide the UITextField from sight like so:
func viewDidLayoutSubviews(){
// Here is just an EXAMPLE of what I'd do with my text field, you can change this however you wish
// The point is that I am putting it away so no one can see it at first, and then later it will show
joinTextField.center = CGPointMake(joinTextField.frame.origin.x-500, joinTextField.frame.origin.y)
}
Now the textfield should be hidden when the view loads and then it can be animated later on.I hope this helped.

Resources