Swift : addSubview before StoryBoard - ios

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.

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

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

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.

Objective-C Tab view controller move tab bar to top

I create a tabviewcontroller in story board, I'm using Objective-C.
How can I move the position of the tab bar to the top?
I found this code in swift how can it write it in Objective-C?
okay! cool.
Are you experienced in swift?
do you know how can i write this in Objective-C?
Swift3: I achieve this by creating a custom class for UITabBarController:
class CustomTabBarController: UITabBarController {
#IBOutlet weak var financialTabBar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
// I've added this line to viewDidLoad
UIApplication.shared.statusBarFrame.size.height
financialTabBar.frame = CGRect(x: 0, y:
financialTabBar.frame.size.height, width:
financialTabBar.frame.size.width, height:
financialTabBar.frame.size.height)
}
There's no supported way to do that with UITabBarController.
If you want a tab bar at the top of the screen, you'd have to build this yourself, or use an open source implementation.
You may find something here but there are other implementations (on GitHub etc.).
Having the tab bar at the top is an Android thing. On iOS it's always been at the bottom.
It's important to consider that all important apps (including those from Apple) have the tab bar at the bottom. Putting it at the top would mean that your app immediately feels weird for iOS users.
If you are satisfied with your swift version then below is the translated objective-c code:
- (void)viewDidLoad{
[super viewDidLoad];
self.tabBar.frame = CGRectMake( 0, [UIApplication sharedApplication].statusBarFrame.size.height, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
}

Preload UIWebView

I've tried in my code to preload an UIWebView but without any result.
I can't figure out what is wrong in the code.
First UIViewController
func viewDidLoad(){
let gallery = storyboard?.instantiateViewControllerWithIdentifier("Gallery") as! GalleryViewController
gallery.loadwWebView()
}
In the second UIViewController
func loadwWebView(){
let webView = UIWebView(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: view.bounds.size.height))
webView.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.google.com")!))
self.view.addSubview(webView)
}
I've choose the programmatically to create the UIWebView way because if I use the #IBOutlet its gonna crash.
The problem is that you're instantiating a gallery, which is in turn creating a web view, but gallery is a local variable and will be deallocated when it falls out of scope.
You need to keep a reference to this view controller (e.g. make it a property of the current view controller). If you do this, you need to make sure that when you transition to this view controller, you don't use a segue (because that will create a second instance of that second view controller), but rather programmatically transition to the one you already instantiated.
You might also want to save a reference to that web view you added to the second view controller's view hierarchy because otherwise you won't be able to reference it again (or at least not without some cumbersome enumeration of the subviews).

Resources