Say me please how to add a ProgressView under navigation bar?
I try to use solution in this post: adding progress bar under navigation bar, but there code was written on ObjectiveC language... I try to translate to Swift. This is the code, which i added in my NavigationController SubClass
import UIKit
class CustomNavigationController: UINavigationController {
#IBOutlet var Secondprogress: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()
NSLayoutConstraint.deactivateConstraints(self.view.constraints())
Secondprogress?.setTranslatesAutoresizingMaskIntoConstraints(false)
var navBar = self.navigationController?.navigationBar
Secondprogress.tag = 1
self.view.addSubview(Secondprogress)
var Constraint = NSLayoutConstraint(item: self.Secondprogress,
attribute:NSLayoutAttribute.Bottom,
relatedBy:NSLayoutRelation.Equal,
toItem:navBar,
attribute:NSLayoutAttribute.Bottom,
multiplier:1.0,
constant:-0.5);
self.view.addConstraint(Constraint);
Constraint = NSLayoutConstraint(item: self.Secondprogress,
attribute:NSLayoutAttribute.Left,
relatedBy:NSLayoutRelation.Equal,
toItem:navBar,
attribute:NSLayoutAttribute.Left,
multiplier:1.0,
constant:0);
self.view.addConstraint(Constraint);
Constraint = NSLayoutConstraint(item: self.Secondprogress,
attribute:NSLayoutAttribute.Right,
relatedBy:NSLayoutRelation.Equal,
toItem:navBar,
attribute:NSLayoutAttribute.Right,
multiplier:1.0,
constant:0);
self.view.addConstraint(Constraint);
Secondprogress.setTranslatesAutoresizingMaskIntoConstraints(false)
Secondprogress.hidden = false
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
But when i compile my app, i don't see the ProgressView under Navigation Bar.
Where is my mistake?
My problem was solved.
Add the Progress View to View Controller.(Drag and drop)
Make a IBOutlet.
Write the code:
override func viewDidLoad() {
super.viewDidLoad()
var navBar = self.navigationController?.navigationBar
var navBarHeight = navBar?.frame.height
var ProgressFrame = self.Progress.frame
var pSetX = ProgressFrame.origin.x
var pSetY = CGFloat(navBarHeight!)
var pSetWidth = self.view.frame.width
var pSetHight = ProgressFrame.height
Progress.frame = CGRectMake(pSetX, pSetY, pSetWidth, pSetHight)
self.navigationController?.navigationBar.addSubview(Progress)
Progress.setTranslatesAutoresizingMaskIntoConstraints(false)
}
4.Success!
Have a look at http://www.appcoda.com/webkit-framework-intro/ - below the "Displaying Progress" part.
It's written in Swift, but they use interface builder to create the constraints.
Related
enter image description hereSo i have backgroundView (UIView) and two buttons in it.
Now i have made following code
buttonBackgroundView.layer.cornerRadius = 5.0
buttonBackgroundView.layer.borderColor = UIColor.black.cgColor
buttonBackgroundView.layer.borderWidth = 0.5
#IBOutlet weak var firstbutton: UIButton!{
didSet{
proceedButton.clipsToBounds = true
}
}
in this the view has rounded corner but the button does not have those rounded corners
How do i implement that? (so that button also get the rounded corner.
I have two buttons, so I want only the left corner of left button and right corner of right button.
I have added the image link which i want to achieve.
class ViewController: UIViewController {
#IBOutlet weak var yourView: UIView! //Create your view outlet
override func viewDidLoad() {
super.viewDidLoad()
yourView.clipsToBounds = true
yourView.layer.cornerRadius = 15 // As per you requirement
yourView.layer.borderColor = UIColor.black.cgColor
yourView.layer.borderWidth = 2
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Add Following constraint
Output:
This might help you :)
Please try using masksToBounds property,
#IBOutlet weak var firstbutton: UIButton!{
didSet{
proceedButton.clipsToBounds = true
proceedButton.masksToBounds = true
}
}
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var vsuper: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let v = view2.getView()
vsuper.backgroundColor = UIColor.black
vsuper.addSubview(v)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//xib file
import UIKit
class view2: UIView {
override func awakeFromNib() {
super.awakeFromNib()
}
static func getView()->view2{
let v = Bundle.main.loadNibNamed("view2", owner: nil, options: nil)?.first as! consentview
// v.translatesAutoresizingMaskIntoConstraints = false
return v;
}
}
While I load xib into another view in story board it doesn't get added inside that view if translateautoresizingmaskintoconstraints is set to false but if I remove that line it gets added to the view.
if I set it to false it takes space on top left otherwise it gets added inside the view. Why so ? even though I am adding it tov super
You did not set the constraints. translatesAutoresizingMaskIntoConstraints = false means you don't want the xib frame to be translated into constraints thus you will setup constraints yourself. Thats why its not assigning any bounds to the xib view on your superview. Try making some constraints with the superview after addSubview() call. such as,
v.leadingAnchor.constraint(equalTo: vsuper.leadingAnchor, constant: 0).isActive = true
v.trailingAnchor.constraint(equalTo: vsuper.trailingAnchor, constant: 0).isActive = true
v.topAnchor.constraint(equalTo: vsuper.topAnchor, constant: 0).isActive = true
v.bottomAnchor.constraint(equalTo: vsuper.bottomAnchor, constant: 0).isActive = true
vsuper.layoutIfNeeded()
Try it
import UIKit
class MyView: UIView {
// your outlets from your view can be there
// your functions for your view
func myFunc() {
}
}
import UIKit
class ViewController: UIViewController {
var myView: MyView!
override func viewDidLoad() {
super.viewDidLoad()
if let contentView = Bundle.main.loadNibNamed("MyView", owner: self, options: nil)?.first as? MyView {
myView = contentView
self.view.addSubview(myView)
}
// set background color your custom view
myView.backgroundColor = UIColor.black
// call functions for your custom view
myView.myFunc()
}
}
I need to change color of UIView then I tap on it. How I can do this?
P.S. Sorry for my english
here are the steps.
Drag a UIView to your ViewController
Then create an IBOutlet to it. (just drag a connection to your viewcontroller class).
then create gesture for it.
then create an action for it.
assign the action
here is the full code for your view controller
import UIKit
class ViewController: UIViewController {
//Outlet Connection to your View
#IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
myView.layer.borderColor = UIColor.black.cgColor
myView.layer.borderWidth = 2
let taptoColorGesture = UITapGestureRecognizer.init(target: self, action: #selector(ViewController.changeMyColor))
myView.addGestureRecognizer(taptoColorGesture)
// Do any additional setup after loading the view, typically from a nib.
}
func changeMyColor () {
let colorR : Float = Float(Int(arc4random_uniform(255)))
let colorG : Float = Float(arc4random_uniform(255))
let colorB : Float = Float(arc4random_uniform(255))
let themyColor = UIColor.init(colorLiteralRed: colorR/255, green: colorG/255, blue: colorB/255, alpha: 1.0)
myView.backgroundColor = themyColor
//this function creates random color each time
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
and here is the project url to github Change the UIView Color on tap in Swift
// first go to identity inspector and set 'user interaction enabled' on the view
// Then put this in the viewDidLoad method (change :myExampleView to your view name)
let tapChangeColor = UITapGestureRecognizer(target: self, action: #selector(changeColor))
self.myExampleView.addGestureRecognizer(tapChangeColor)
// put this method somewhere in ViewController class:
func changeColor() {
self.myExampleView.backgroundColor=UIColor.blue
}
I have created common sidemenu for all main view controller using reference https://github.com/evnaz/ENSwiftSideMenu
Now the issue is i have created sidemenu view controller from storyboard instead of using code itself,it will not show anything on side menu.
Ideally it has to show the page which design from story board.
Actually only TableViewController work with this example. i need to work with UIViewController.
Anyone have idea about this ?
Check out the latest version, I added precisely that functionality a few weeks ago: you can now use a UIViewController, no need to use a UITableViewController.
But other than that, I can't tell without more information why it doesn't show up.
I'm using it in several apps and it works ok.
I've got a UINavigationController, which uses a subclass of ENSideMenuNavigationController, and a UIViewController for the menu itself.
This is it, basically:
class MainNavigationController: ENSideMenuNavigationController, ENSideMenuDelegate {
override func viewDidLoad() {
super.viewDidLoad()
var mainMenuViewController: MainMenuViewController = storyboard?.instantiateViewControllerWithIdentifier("MainMenuViewController") as! MainMenuViewController
mainMenuViewController.navController = self
sideMenu = ENSideMenu(sourceView: self.view, menuViewController: mainMenuViewController, menuPosition:.Right)
//sideMenu?.delegate = self //optional
sideMenu?.menuWidth = 240.0 // optional, default is 160
sideMenu?.bouncingEnabled = false
sideMenu?.animationDuration = 0.2
// make navigation bar showing over side menu
view.bringSubviewToFront(navigationBar)
}
// MARK: - ENSideMenu Delegate
func sideMenuWillOpen() {
println("sideMenuWillOpen")
}
func sideMenuWillClose() {
println("sideMenuWillClose")
}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
super.didRotateFromInterfaceOrientation( fromInterfaceOrientation )
sideMenu?.updateFrame()
}
}
Then I have the menu view itself, also in the Storyboard, which is a UIViewController. Here is a fragment:
class ERAMainMenuViewController: UIViewController {
weak var navController: ERAMainNavigationController?
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var exitButton: UIButton!
#IBOutlet weak var headImage: UIImageView!
let kInset:CGFloat = 64.0
override func viewDidLoad() {
super.viewDidLoad()
// Customize apperance of table view
tableView.contentInset = UIEdgeInsetsMake(kInset, 0, 0, 0) //
tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
tableView.backgroundColor = ERAssistantTheme.sideMenuItemBackgroundColor
tableView.scrollsToTop = false
// Preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = true
// tableView.selectRowAtIndexPath(NSIndexPath(forRow: selectedMenuItem, inSection: 0), animated: false, scrollPosition: .Middle)
}
}
I'm trying to make a scroll view on iOS with Swift and Autolayout, but the screen crop the content instead of resizing it.
My layout and constraints
How can i fix this ?
EDIT : Tried with UITextView it doesn't work too
EDIT 2 : I'm trying to make a screen like that and my problem is that i can't implements this screen for all screens resolution and rotation with AutoLayout
I found a solution !
Use Interface Builder only to add a UIScrollView and add constraints to fit superview, and create all subviews programatically.
Override updateViewConstraints()and for each view in the ScrollView set translatesAutoresizingMaskIntoConstraints to false and add a width constraint to keep scrollview width. Dont forget to call super.updateViewConstraint().
Override didRotateFromInterfaceOrientation() and viewDidAppear() and set scrollView.contentSize.height to wrap all items (if you don't do that, you can't scroll).
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
var constraintsUpdated = false;
var label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
label.numberOfLines = 0
label.textColor = UIColor.blackColor()
scrollView.addSubview(label)
}
override func updateViewConstraints() {
if(constraintsUpdated){
//Rotate, just update view and keep constraints
super.updateViewConstraints()
scrollView.layoutSubviews()
return;
}
var viewsDictionary = ["label": label, "scrollView": scrollView]
let screenWidth = self.view.frame.width
label.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(String(format: "H:[label(==scrollView)]", screenWidth), options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary))
constraintsUpdated = true
super.updateViewConstraints()
//Update subviews
scrollView.layoutSubviews()
}
override func viewDidAppear(animated: Bool) {
scrollView.contentSize.height = label.bounds.height
}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
scrollView.contentSize.height = label.bounds.height
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I found another solution with FLKAutoLayout library, that's work great with less code!
class ViewController: UIViewController {
var labelTitle: UILabel = UILabel()
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
//TITLE
scrollView.addSubview(labelTitle)
labelTitle.textColor = UIColor.blackColor()
labelTitle.numberOfLines = 0
//constraints
labelTitle.constrainWidthToView(self.view, predicate: nil)
labelTitle.numberOfLines = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews() {
scrollView.contentSize.height = labelTitle.frame.height
}
}
I don't know if it's a UIScrollView bug with AutoLayout or a feature