I am using SFSafariViewController, and as i did not wanted to show the status bar of safari and i have hidden it by adding a overlay. I have my own button on the overlay and its working fine. The safari status bar is hidden and it shows my overlay but somehow sometimes the overlay disappears and the safari status bar is visible. I doubt that its because the link is being reloaded automatically but how? I am only calling it once. I am not sure why overlay is disappear for sometimes only. below is my code -
self.navigationController?.presentViewController(sfController!, animated: true, completion: {
let bounds = UIScreen.mainScreen().bounds
let overlay = UIView(frame: CGRect(x: 0, y: 0, width: bounds.size.width, height: 65))
overlay.backgroundColor = UIColor(netHex: 0x275E37)
let completedBtn = UIButton()
let favBtn = UIButton()
let image = UIImage(named: "home.png") as UIImage?
let homeBtn = UIButton(type: UIButtonType.Custom) as UIButton
homeBtn.frame = CGRectMake(20, 25, 35, 35)
homeBtn.setImage(image, forState: .Normal)
homeBtn.addTarget(self, action: #selector(DetailsViewController.HomeBtnAction), forControlEvents:.TouchUpInside)
completedBtn.setTitle("Mark as Completed",forState: .Normal)
completedBtn.titleLabel!.font = UIFont(name: "FidelitySans-Regular", size: 20)
completedBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
completedBtn.frame = CGRectMake((bounds.size.width - 210), 25, 200, 35)
completedBtn.backgroundColor = UIColor(netHex: 0x6F9824)
completedBtn.addTarget(self, action: #selector(DetailsViewController.CompletedAction), forControlEvents: UIControlEvents.TouchUpInside)
favBtn.setTitle("Save as Favorite", forState: .Normal)
favBtn.titleLabel!.font = UIFont(name: "FidelitySans-Regular", size: 20)
favBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
favBtn.frame = CGRectMake((bounds.size.width - 420), 25, 200, 35)
favBtn.backgroundColor = UIColor(netHex: 0x6F9824)
favBtn.addTarget(self, action: #selector(DetailsViewController.FavoriteAction), forControlEvents: UIControlEvents.TouchUpInside)
overlay.addSubview(favBtn)
overlay.addSubview(completedBtn)
overlay.addSubview(homeBtn)
self.sfController!.view.addSubview(overlay)
})
Any help will be much appreciated as i am stuck in this issue from long time and cant fine solution. Thanks!
I got solution by adding overlay in the safariViewController delegate method didCompleteInitialLoad. So a indicator with transparent dark background until the didCompleteInitialLoad method is not called, once the URL is fully loaded didCompleteInitialLoad will be called and not add the overlay to safariviewcontroller in this method. like below code so that its always on the top of the stack.
self.sfController?.view.insertSubview(overlay, atIndex: (self.sfController?.view.subviews.count)!)
What you are trying to do is not allowed. SFSafariViewController should always be presented as it is.
Instead, you could use WKWebView to build an in-app browser and add custom controls.
Related
I'm using SJSegmentedViewController to make my view controller segmented.
I have one view controller named as PlaceReviewsVC where I have added textView for review. For post this review I'm adding Post bar button instance of UIBarButtonItem at navigationItem.rightBarButtonItem in my placeDetailSegmentController which are instance of SJSegmentedViewController.
Now issue is when I click on post button, I'm not able to dismiss keyboard.
I have tried following code to add post button in segmented view controller.
let headerVC = PlaceImagesVC.viewController()
let reviewVC = PlaceReviewsVC.viewController()
reviewVC.delegate = self
reviewVC.title = "Reviews"
let placeDetailSegmentController = SJSegmentedViewController()
placeDetailSegmentController.delegate = self
placeDetailSegmentController.title = "Pedal Studio"
placeDetailSegmentController.headerViewController = headerVC
placeDetailSegmentController.segmentControllers = [reviewVC]
placeDetailSegmentController.headerViewHeight = 280
placeDetailSegmentController.selectedSegmentViewHeight = 2.0
placeDetailSegmentController.selectedSegmentViewColor = .white
let button = UIButton(type: .custom)
button.setImage(#imageLiteral(resourceName: "icon_back").withRenderingMode(.alwaysTemplate), for: .normal)
button.tintColor = .white
button.addTarget(self, action:#selector(backButtonTapped), for:.touchUpInside)
button.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
button.contentMode = .scaleAspectFit
button.imageEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
let leftButton = UIBarButtonItem(customView: button)
placeDetailSegmentController.navigationItem.leftBarButtonItem = leftButton
let btnPost = UIButton(type: .custom)
btnPost.setTitle("POST", for: .normal)
btnPost.addTarget(self, action:#selector(btnPostTapped), for:.touchUpInside)
btnPost.titleLabel?.setFont(Typography.getFont(.PoppinsMedium, size: 12.0), .white)
btnPost.setTitleColor(.menuColor, for: .normal)
btnPost.setTitleColor(.darkGray, for: .highlighted)
btnPost.contentMode = .scaleAspectFit
let rightButton = UIBarButtonItem(customView: btnPost)
placeDetailSegmentController.navigationItem.rightBarButtonItem = rightButton
self.navigationController?.pushViewController(placeDetailSegmentController, animated: true)
where inside btnPostTapped method I have wrote following code to dismiss keyboard. But didn't work.
#objc func btnPostTapped() {
self.view.endEditing(true)
}
How can I dismiss keyboard from one view controller to another?
You should change your code to dismiss keyboard as follows:
#objc func btnPostTapped() {
AppDelegate.shared.window?.endEditing(true)
}
Our app's base is window, So when you perform any event on window it will affect on whole app. I hope this will fix your issue.
Hide keyboard from anywhere
UIApplication.shared.sendAction(#selector(UIApplication.resignFirstResponder), to: nil, from: nil, for: nil)
or
Get the required viewcontroller instance from navigation hierarchy and resign keyboard as:
requiredViewController.textField.resignFirstResponder()
or
requiredViewController.view.endEditing(true)
I'm trying to make the title of the nav bar its own button. Users in my app can have multiple profiles, and the nav bar title displays the username of their currently selected profile. Pressing this button should bring up a list of available profiles to choose from (handleShowProfiles). For some reason, the title displays but does not function as a button, it's just static text and touching it does nothing.
let changeProfileContainer : UIView = {
let container = UIView()
container.frame = CGRect(x: 0, y: 0, width: 200, height: 40)
let button = UIButton(type: .custom)
button.setTitle("#username ▼", for: .normal)
button.setTitleColor(.black, for: .normal)
button.frame = container.frame
button.addTarget(self, action: #selector(handleShowProfiles), for: .touchUpInside)
container.addSubview(button)
return container
}()
func configureNavBar() {
self.navigationController?.navigationBar.tintColor = .black
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "send"), style: .plain, target: self, action: #selector(handleSubmitPost))
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "drafts"), style: .plain, target: self, action: #selector(handleDrafts))
self.navigationItem.titleView = changeProfileContainer
}
Any ideas why the button part won't function? In Apple's documentation, it says you have to put the button inside a view, make the button type custom, and change the button's frame from it's default (0, 0, 0, 0). I'm pretty sure this is where I'm messing up but I dunno.
Linked to self invocations in computed property -- See the last portion of Ahmad F's answer
Don't know why, but selectors in computed properties don't seem to work.
I tried adding the container view without computing it and clicking on the button works.
func configureNavBar() {
self.navigationController?.navigationBar.tintColor = .black
let container = UIView()
container.frame = CGRect(x: 0, y: 0, width: 200, height: 40)
let button = UIButton(type: .custom)
button.setTitle("#username ▼", for: .normal)
button.setTitleColor(.black, for: .normal)
button.frame = container.frame
button.addTarget(self, action: #selector(pressTitle), for: .touchUpInside)
container.addSubview(button)
navigationItem.titleView = container
}
I am creating an sample app which integrated with library named ZFRippleButton . Now i am facing problem which when i tapped on button then image has been disappeared but when i take my finger out then the image is back as normal.
What is the problem?
Here is what i have done so far:
let button = ZFRippleButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.layer.cornerRadius = 5
button.rippleColor = UIColor.red
button.backgroundColor = UIColor.red
button.setTitle("File Server", for: .normal)
button.setImage(UIImage(named: "filesharing"), for: .normal)
button.titleLabel?.font = UIFont(name: "SFCompactText-Regular", size: 16)
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
Here is its action:
button.addTarget(self, action: #selector(fileShareButton), for: .touchUpInside)
#objc
func fileShareButton() {
print("Doing some actions")
}
I cannot figure it out is that when i tapped on that button and then image has been disappeared and when i released my finger and image has appeared as usual. But title never disappear like image.
Can you solve me with this issue?
I have a problem.
I use show(vc) to present a view controller.
vc1 --> vc2 --> vc3
I want to redirect user from vc1 when user clicks on back button on vc3, to achieve that I am using the below mentioned code.
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "<", style: UIBarButtonItemStyle.plain, target: self, action: #selector(backToRoot(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton
}
func backToRoot(sender: UIBarButtonItem) {
_ = self.navigationController?.popToRootViewController(animated: true)
}
I want the default image of back button, not customized button.
Is there a way I can do that?
I just made this today. I found no way to change action of system back button. In order to look like the system style, I draw a same picture(snapshot the simulator, and change in photo shop). The code is as followed:
let button: UIButton = UIButton(type: .system)
the default size of height is 30, width is whatever you set
button.frame = CGRect(x: 0, y: 0, width: 80, height: 30)
button.setImage(UIImage(named: "BackButton"), for: .normal)
the default font is the following:
button.setTitle("Back", for: .normal)
button.titleLabel?.font = UIFont(name: "AvenirNext", size: 15)
This is the trick, you need find out the same position with the system's, the following is my result, I had adjusted many times. I hope you could use, but I think you need find your own size
button.imageEdgeInsets = UIEdgeInsetsMake(0, -43, 0, 0)
button.titleEdgeInsets = UIEdgeInsetsMake(0, -33, 0, 0)
button.addTarget(target, action: action, for: .touchUpInside)
let barButton = UIBarButtonItem(customView: button)
I've got this transparent Navigation bar, and I can add a custom icon (code below). However it does not seem to respond to clicks, anyone have any idea why this could be caused?
I'm adding this inside a UIViewController which is inside a UINavigationController.
var button: UIButton = UIButton()
button.setImage(UIImage(named: "customBack"), forState: .Normal)
button.frame = CGRectMake(0, 0, 40, 40)
button.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
button.targetForAction("doAction:", withSender: button)
button.backgroundColor = UIColor.brownColor()
var leftItem:UIBarButtonItem = UIBarButtonItem()
leftItem.customView = button
self.navigationItem.leftBarButtonItem = leftItem
self.navigationItem.leftBarButtonItem!.action = "doAction:"
self.navigationController!.navigationItem.leftBarButtonItem = leftItem
I know I shouldn't be adding back items myself but in this exact case it is needed. The icon is added on the fly not before loading the view or w/e.
To try it...
var ysBackButton:UIButton = UIButton(frame:CGRectMake(0, 0, kNavBtnSize, kNavBtnSize))
ysBackButton.setImage(UIImage(named: kBack), forState: UIControlState.Normal)
ysBackButton.addTarget(self, action: Selector("backButtonAction"), forControlEvents:UIControlEvents.TouchUpInside)
var leftBarButton:UIBarButtonItem = UIBarButtonItem(customView: ysBackButton)
self.navigationItem.leftBarButtonItem = leftBarButton
Always just after posting I find out:
I was using
button.targetForAction("doAction:", withSender: button)
Whilst I should be doing:
button.addTarget(self, action: "doAction:", forControlEvents: .TouchUpInside)