I am having that issue when I using PageMenu Framework and put all code for the PageMenu but its showing white space in my controller. Anybody have the solutions for that type of issue.let me know how can I remove it.
Blow I put complete code for PageMenu
import UIKit
import PageMenu
class TradingHistoryVC: UIViewController {
#IBOutlet weak var pagerView: UIView!
var controllerArray : [UIViewController] = []
var pageMenu : CAPSPageMenu?
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.topItem?.title = ""
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
pager()
}
func pager()
{
let buy = storyboard?.instantiateViewController(withIdentifier: "TradingSuccessfullyVCSID") as! TradingSuccessfullyVC
buy.parentNavigationController = navigationController!
buy.title = "Trading Successfully"
controllerArray.append(buy)
let sell = storyboard?.instantiateViewController(withIdentifier: "GlobalTradingVCSID") as! GlobalTradingVC
sell.parentNavigationController = navigationController!
sell.title = "Global Trading"
controllerArray.append(sell)
// Customize menu (Optional)
let parameters: [CAPSPageMenuOption] = [
.scrollMenuBackgroundColor(UIColor(red: 72/255.0, green: 175/255.0, blue: 230/255.0, alpha: 1.0)),
.viewBackgroundColor(UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)),
.selectionIndicatorColor(UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1.0)),
.addBottomMenuHairline(false),
.menuItemFont(UIFont(name: "Times New Roman", size: 16)!),
.menuItemWidth((self.pagerView.frame.width-30)/2),
.menuHeight(50.0),
.enableHorizontalBounce(true),
.menuItemWidthBasedOnTitleTextWidth(false),
.selectedMenuItemLabelColor(UIColor.white),
.unselectedMenuItemLabelColor(UIColor.white),
.menuItemSeparatorWidth(4.5),
.useMenuLikeSegmentedControl(false),
.menuItemSeparatorRoundEdges(true),
.selectionIndicatorHeight(2.0),
.menuItemSeparatorPercentageHeight(0.0),
]
// Initialize scroll menu
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRect(x: 0.0, y: 0.0, width: self.pagerView.frame.width, height: self.pagerView.frame.height), pageMenuOptions: parameters)
self.pagerView.addSubview(pageMenu!.view)
// End Pagemenu
}
#IBAction func btnMenuAction(_ sender: Any) {
toggleSideMenuView()
}
}
If anyone have its solutions plz. Please Help to solve it.
Related
i am creating using Swift in my app i am implementing XLPagerTabStrip and i have referred below blog to implement here is the link
This is the reference link
all thing works perfect although when i navigate to my Home screen then XLPagerTabStrip showing over status bar here is the screenshot what type of output i am getting
This is the screen shot of output which i am getting
This is expected output i want something like this
This is storyboard screenshot
here is my Controller code
import UIKit
import XLPagerTabStrip
class HomeViewController: ButtonBarPagerTabStripViewController {
//MARK: - VARIABLEs
//MARK: - OUTLETs
//MARK: - VOID METHODs
override func viewDidLoad() {
configureButtonBar()
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//MARK: - CLICKED EVENTs
//MARK: - FUNCTIONs
func configureButtonBar() {
settings.style.buttonBarBackgroundColor = .white
settings.style.buttonBarItemBackgroundColor = .white
settings.style.buttonBarItemFont = UIFont(name: "Roboto-Medium", size: 14.0)!
settings.style.buttonBarItemTitleColor = UIColor(red: 84/255, green: 84/255, blue: 84/255, alpha: 1)
settings.style.buttonBarMinimumLineSpacing = 0
settings.style.buttonBarItemsShouldFillAvailableWidth = true
settings.style.buttonBarLeftContentInset = 0
settings.style.buttonBarRightContentInset = 0
settings.style.selectedBarHeight = 3.0
settings.style.selectedBarBackgroundColor = UIColor(red: 112/255, green: 112/255, blue: 112/255, alpha: 1)
// Changing item text color on swipe
changeCurrentIndexProgressive = { [weak self] (oldCell: ButtonBarViewCell?, newCell: ButtonBarViewCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
guard changeCurrentIndex == true else { return }
oldCell?.label.textColor = UIColor(red: 84/255, green: 84/255, blue: 84/255, alpha: 1)
newCell?.label.textColor = UIColor(red: 112/255, green: 112/255, blue: 112/255, alpha: 1)
}
}
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
let child1 = UIStoryboard.init(name: "Functional", bundle: nil).instantiateViewController(withIdentifier: "FeedViewController") as! FeedViewController
let child2 = UIStoryboard.init(name: "Functional", bundle: nil).instantiateViewController(withIdentifier: "ArticlesViewController") as! ArticlesViewController
return [child1, child2]
}
}
i did everything same as provided in demo app but still XLPagerTabStrip covers status bar and view which contain notification and menu icon i can't get it why this is happing i did everything as in demo can anyone have idea about this
as you can see in storyboard screenshot i have added menu and notification icon but XLPagerTabStrip covers that whole view when run the app
can anyone have idea why this is happing then please suggest me
Please read the blog from the official team.
XLPagerTabStrip tutorial
I am following this tutorial provided on Youtube for: How to Save Data with UserDefaults - Swift
https://www.youtube.com/watch?v=sUhq1vIrRbo
And I have this code that works for one page only and would like to know how to do the same exact thing (changing background color) but for my entire app pages based on the user's choice.
I have tried keeping the checkForStylePreference() in the viewDidLoad()of another page but it did not recognize it. I copy pasted the whole checkForStylePreference() but still other pieces of code were missing. Is the only way to do it is by copy pasting all of the methods of the viewController in all App pages? Or there is a much simpler way as a believe to reduce amount of code? Currently I can change BgColor from white to grey perfectly enter image description here but I don't know how to apply it for all.
This is the code of my NameViewController.swift (the one I've created for the page in the screenshot). Please note that I have 2 more swift files which are SAButton.swift and ConstantStyles.swift (for the colors)
class NameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
nameLbl.text = myString
checkForStylePreference()
}
#IBAction func didChangeStyleSeg(_ sender: UISegmentedControl) {
isDarkMode = sender.selectedSegmentIndex == 1
saveStylePreference()
updateStyle()
}
var myString = String()
#IBOutlet weak var styleSegment: UISegmentedControl!
#IBOutlet weak var nameLbl: UILabel!
var isDarkMode = false
let defaults = UserDefaults.standard
struct Keys {
static let preferDarkMode = "preferDarkMode"
}
func updateStyle(){
UIView.animate(withDuration: 0.4){
// self.view.backgroundColor = self.isDarkMode ? Colors.darkGrey : .white
// UIColor(hue: 287/360, saturation: 15/100, brightness: 85/100, alpha: 1.0)
self.view.backgroundColor = self.isDarkMode ? Colors.lightGrey : .white
//recent correct one
// self.view.backgroundColor = self.isDarkMode ? Colors.darkGrey : .white
//self.view.UIBackgroundFetchResult = self.isDarkMode? UIColor.grey : .white
}
}
func saveStylePreference(){
defaults.set(isDarkMode, forKey: Keys.preferDarkMode)
}
func checkForStylePreference(){
let preferDarkMode = defaults.bool(forKey: Keys.preferDarkMode)
if preferDarkMode{
isDarkMode = true
updateStyle()
styleSegment.selectedSegmentIndex = 1
}
}
// Do any additional setup after loading the view.
}
Code of the SAButton.swift
class SAButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
setupButton()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupButton()
}
private func setupButton() {
setTitleColor(.white, for: .normal)
backgroundColor = Colors.lightBlue
titleLabel?.font = .boldSystemFont(ofSize: 20)
layer.cornerRadius = frame.size.height / 2
}
}
Code of the ConstantStyles.swift
import UIKit
struct Colors {
static let darkGrey = UIColor(red: 40/255, green: 40/255, blue: 40/255, alpha: 1)
// static let purple = UIColor(red: 212/255, green: 186/255, blue: 86/255, alpha: 1)
static let lightBlue = UIColor(red: 89/255, green: 205/255, blue: 242/255, alpha: 1)
static let darkPurple = UIColor(red: 242/255, green: 232/255, blue: 255/255, alpha: 1.0)
// UIColor(hue: 287/360, saturation: 15/100, brightness: 85/100, alpha: 1.0)
static let lightPurple = UIColor(red: 240/255, green: 229/255, blue: 255/255, alpha: 1.0)
static let lightGrey = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1.0)
//UIColor(red: 249/255, green: 244/255, blue: 255/255, alpha: 1.0)
}
I believe it could be simple but I am new to Swift, I would like to know what part of code to keep exactly and where. Much appreciated.
Ps: Original project Source Code is provided below the Youtube Video.
You can create a main class and inherit from it
class GeneralVC : UIViewController {
override func viewDidLoad() {
self.view.backgroundColor = .red // read color from userdefaults and set it here
}
}
class ViewController: GeneralVC {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Same applies to any UIKit component that you need to affect globally
Another interesting way to do it is to use Appearance:
Perhaps you can use UIViewControllerWrapperView as a parent.
UIView.appearance(whenContainedInInstancesOf: [UIViewControllerWrapperView]) // UIViewControllerWrapperView might be private. In that case it might take some wizardry to get it to work
Another way to do it is to set it when the UITabBarController or UINavigationController presents a new UIViewController. You can do this by subclassing them.
The reason why I don't like subclassing is that you force a subclass for just one simple thing. If you only do it in a few navigation based ones it's much easier and also easier to override with extensions instead of everything through subclassing.
I've got a MainViewController where I'm loading a slideout-menu. The MainViewController contains a static TopView and a ContentView where different child controllers or views are loaded, depending on which menu entry was selected.
There is a button inside one of the children is loaded, but it is not working.
When I start the app with the ViewController including the button, set to "is initial View Controller", everything works properly.
When starting the app with the MainViewController as initial View Controller with a loaded child, the button is not working (it is loaded in the view, but not reacting).
Any suggestions how I can make this work?
MainViewController loading of Child View
class MainViewController: UIViewController, SideBarDelegate {
var contentView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
....
contentView.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
contentView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(contentView)
contentView.topAnchor.constraint(equalTo: statusBarView.bottomAnchor).isActive = true
contentView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
contentView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
contentView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
contentView.layoutIfNeeded()
}
func sideBarDidSelectButtonAtIndex(_ index: Int) {
...
if index == 0{
statusBarLabel.text = "First"
let controller:FirstViewController = storyboard!.instantiateViewController(withIdentifier: "First") as! FirstViewController
controller.view.frame = ContentView.bounds
controller.willMove(toParentViewController: self)
contentView.addSubview(controller.view)
controller.didMove(toParentViewController: self)
print("touched index 0")
} else if index == 1{
// index is 1
}
}
FirstViewController
class FirstViewController: UIViewController, UIScrollViewDelegate {
let addButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
addButton.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60)
addButton.clipsToBounds = true
addButton.setTitleColor(UIColor.white, for: .normal)
addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
addButton.setTitle("add feed", for: .normal)
addButton.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: UIFontWeightRegular)
self.view.addSubview(AddButton)
addButton.layoutIfNeeded()
addButton.addTarget(self, action: #selector(touchUpAddButton), for: UIControlEvents.touchUpInside)
addButton.addTarget(self, action: #selector(touchDownAddButton), for: UIControlEvents.touchDown)
func touchUpAddButton() {
addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
}
func touchDownAddButton() {
addButton.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
}
}
While adding the view in the container you don't have to use addSubview function, that the FirstViewController functionality is not working.
Use below code:
let newViewController:FirstViewController = storyboard!.instantiateViewController(withIdentifier: "First") as! FirstViewController
let oldViewController = childViewControllers.last! as UIViewController
oldViewController.willMove(toParentViewController: nil)
addChildViewController(newViewController)
transition(from: oldViewController, to: newViewController, duration: 0.25, options: .transitionCrossDissolve, animations:{ () -> Void in
// nothing needed here
}, completion: { (finished) -> Void in
oldViewController.removeFromParentViewController()
newViewController.didMove(toParentViewController: self)
})
You can change the duration, it used for animation
Update your functions to these
func TouchUpAddButton(sender: UIButton){
addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
}
func TouchDownAddButton(sender: UIButton) {
addButton.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
}
And change your code of adding the target to
addButton.addTarget(self, action: #selector(FirstViewController.touchUpAddButton(_:)), for: .touchDown)
addButton.addTarget(self, action: #selector(FirstViewController.touchDownAddButton(_:)), for: .touchDown)
First solution:
Remove an autolayout constrains
Second solution:
Button is not performing an action because a child view controller view should be added directly to a container controller view (in your case: MainViewController view) and not via an intermediate view (in your case: contentView)
I have read several answers on this question but can't seem to find one that applies to my code. When I assign the "text" member of "myLabel" (below) I get: "[UIButton setText:]: unrecognized selector sent to instance...." The program compiles OK but then crashes when the assignment statement is encountered.
However I am able to assign and even print out the backgroundColor of the same object. Any ideas on what I am doing wrong?
//********
import UIKit
class ViewController: UIViewController {
//********* Button 1 *********//
#IBOutlet weak var lbl1: UILabel!
#IBAction func btn1(sender: AnyObject) {
nextColorFor1++
makeColor (self.lbl1, nextColor: nextColor1)
}
//**** Button 2 **********//
#IBOutlet weak var lbl2: UILabel!
#IBAction func btn2(sender: AnyObject) {
nextColorFor2++
makeColor (self.lbl2, nextColor: nextColorFor2)
}
//***************************//
func makeColor (myLabel:UILabel, nextColor:Int)
{
switch nextColor
{
case 0: myLabel.backgroundColor = UIColor(red: 0.1, green: 0, blue: 0, alpha: 0.1)
case 1: myLabel.backgroundColor = UIColor(red: 0.2, green: 0, blue: 0, alpha: 0.2)
case 2: myLabel.backgroundColor = UIColor(red: 0.3, green: 0, blue: 0, alpha: 0.3)
case 3: myLabel.backgroundColor = UIColor(red: 0.4, green: 0, blue: 0, alpha: 0.4)
case 4: myLabel.backgroundColor = UIColor(red: 0.5, green: 0, blue: 0, alpha: 0.5)
default: print("end of colors")
}
print(myLabel.backgroundColor)
myLabel.text = "4" <<< --- This is where the crask occurs
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
It seems like you have connected the UIButton(which is on your storyboard or xib) as as UILabel in the code, and you are trying to treat it as label. Make sure to replace the button on the IB to label, and it will work.
Don't pass the local variable
func makeColor()
{
myLabel.backgroundColor = UIColor(red: 0.1, green: 0, blue: 0, alpha: 0.1)
print(myLabel.backgroundColor)
myLabel.text = "4"
}
#IBAction func myButton(sender: AnyObject) {
makeColor ()
}
Now If you want to set the title for button
#IBOutlet var myButton: UIButton!
#IBAction func myButton(sender: AnyObject)
{
myButton.backgroundColor = UIColor(red: 0.1, green: 0, blue: 0, alpha: 0.1)
myButton.titleLabel?.text = "4"
}
try this to ease the bug in case the origin (xib || storyboard) is unknown
extension UIButton
{
func setText(_ text: String) {
assertionFailure("crashlytics reported Fatal Exception: NSInvalidArgumentException -[UIButton setText:]: unrecognized selector sent to instance 0xyyy objc_exception_throw keyboard_arrow_down")
titleLabel?.text = text
}
}
I made a push function in a button:
#IBAction func prodButton(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var secondViewController = CollectionViewController()
secondViewController = storyboard.instantiateViewControllerWithIdentifier("CollectionViewController") as! CollectionViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
}
This button pushes to the secondViewController but when I looked at the navigation bar of the second view controller I noticed that it has set up a back button automatically. The problem is that this back button's color is light blue and it doesn't fit with my design. I tried to change it like that in viewDidAppear
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.redColor()
and also the bar color:
self.navigationController?.navigationBar.barTintColor = UIColor(red: 65, green: 61, blue: 116, alpha: 1.0)
but there wasn't any change.
I'd be really thankful if somebody help me with that.
Thanks in advance.
Try to set your color one time, it will be the same color for everywhere after:
let navBar = UINavigationBar.appearance()
navBar.tintColor = UIColor.redColor()
You should put this code in AppDelegate. You could also set the barTint in Storyboard if you use it.
Use
//Func Alter color navigation bar
func AlteraCorNavigationBar(Navigation:UINavigationController){
Navigation.navigationBar.tintColor = CorTextoNavigationBar()
//Navigation.navigationBar.barTintColor = CorPredominante()
Navigation.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : CorTextoNavigationBar(), NSFontAttributeName : UIFont(name: "ArialHebrew-Light", size: 22)!]
}
//func CorPredominante()->UIColor{
//return UIColor.redColor()
//(rgba: "#ecf0f1")
// YOU CAN LIBRARY COLOR RGBA [HERE][1] AND USE UIColor(rgba: "#b6011a")
//}
func CorTextoNavigationBar()->UIColor{
return UIColor(red: 65, green: 61, blue: 116, alpha: 1.0)
}
for called in view controller:
class NewViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
AlteraCorNavigationBar(navigationController)
}
}
I found the mistake.
The colors shoud be devided by 255:
UINavigationBar.appearance().tintColor = UIColor(red: 149.0 / 255.0, green: 148.0 / 255.0, blue: 192.0 / 255.0, alpha: 0.5)
UINavigationBar.appearance().barTintColor = UIColor(red: 65.0 / 255.0, green: 61.0 / 255.0, blue: 116.0 / 255.0, alpha: 1.0)
Thank you all for the help ;)