window can't addsubview with swift - swift

In function Appdelegate - didFinishLaunchingWithOptions implement :
var testView : UIView = UIView(frame: CGRectMake(100, 100, 100, 100))
testView.backgroundColor = UIColor.blueColor()
self.window?.addSubview(testView)
self.window?.bringSubviewToFront(testView)
self.window?.makeKeyAndVisible()
But not show testView.

All you need to do is give frame to your window.
and your code will be:
window = UIWindow(frame: UIScreen.mainScreen().bounds)
var testView : UIView = UIView(frame: CGRectMake(0 , 0, 100, 100))
testView.backgroundColor = UIColor.blueColor()
self.window?.addSubview(testView)
self.window?.bringSubviewToFront(testView)
self.window?.makeKeyAndVisible()

Related

Issue reusing the same UI Image code across multiple view controllers | Swift 5

I have the following function I use to customize the navigation bar across almost all the apps view controllers and table view controllers - instead of replicating the code numerous times I am looking for way to easily call the function on those view controllers needing it.
I have tried wrapping in extension UIViewController { } but run into a selector issue saying the following:
Argument of '#selector' cannot refer to local function
'Tapped(tapGestureRecognizer:)'
Code:
func navBar(){
// Profile Image
let containView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.image = UIImage(url: URL(string: "test.com"))
imageView.contentMode = UIView.ContentMode.scaleAspectFit
imageView.layer.cornerRadius = 20
imageView.layer.masksToBounds = true
containView.addSubview(imageView)
let rightBarButton = UIBarButtonItem(customView: containView)
self.navigationItem.rightBarButtonItem = rightBarButton
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
}
#objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
print("Profile Tapped")
}
How can this UIImage be seen in the navigation bar across various view controller without needing to rewrite the same code across all.
Lot a way to do it. I'll usually istance and personalize an UIViewController and use it around the whole app.
class baseController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//call your navBar here
navbar()
}
func navBar(){
// Profile Image
let containView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.image = UIImage(url: URL(string: "test.com"))
imageView.contentMode = UIView.ContentMode.scaleAspectFit
imageView.layer.cornerRadius = 20
imageView.layer.masksToBounds = true
containView.addSubview(imageView)
let rightBarButton = UIBarButtonItem(customView: containView)
self.navigationItem.rightBarButtonItem = rightBarButton
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
}
}
Now instance this class whenever you want and your controller will get your navBar() every time with this
class mineController:baseController {
//your code here...
}

Swift: SegmentedControl in NavBar with Small TitleView

I am attempting to include a segmentedControl on my navBar that looks like this:
The idea here is that the text "fetching..." is a small titleView. However, my current implementation would result in the text "fetching..." on the lower side like so:
I implement large titles so that I can get two "rows" on the navBar, else the word "fetching..." will be hidden behind the segmentedControl.
Code:
let segmentedControl: UISegmentedControl = {
let items = ["Now","In 15 mins", "In 1 hour"]
let sc = UISegmentedControl(items: items)
sc.selectedSegmentIndex = 0
return sc
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem?.title = "Back"
navigationItem.largeTitleDisplayMode = .automatic
navigationItem.titleView = segmentedControl
}
Does anyone have any advice?
You can create a customView that holds all the views you want to show in the navigation bar and set that view as titleView like below,
let segmentedControl: UISegmentedControl = {
let items = ["Now","In 15 mins", "In 1 hour"]
let sc = UISegmentedControl(items: items)
sc.selectedSegmentIndex = 0
return sc
}()
let fetchingLabel: UILabel = {
let label = UILabel(frame: .zero)
label.text = "Fetching..."
return label
}()
In viewDidLoad
let customView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 250))
customView.addSubview(segmentedControl)
customView.addSubview(fetchingLabel)
fetchingLabel.frame = CGRect(x: 150, y: 0, width: self.view.frame.width, height: 60)
segmentedControl.frame = CGRect(x: 60, y: 50, width: self.view.frame.width * 0.75, height: 30)
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .automatic
navigationItem.titleView = customView
This should give you below result. You can play with the values to do what you want.

swift: after adding navigationController to viewController, tableView height is not fitting screen

after adding this codes to appDelegate inside didFinishLaunchingWithOptions function:
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainView = PageViewController(coder: NSCoder())
let nav1 = UINavigationController(rootViewController: mainView!)
nav1.navigationBar.isTranslucent = false
self.window!.rootViewController = nav1
self.window?.makeKeyAndVisible()
tableView height is not looking good, some part of tableView(cells) not fitting screen
my codes for implementing tableView:
let myTableView = UITableView(frame: CGRect(x: 0, y: 40, width: screenSize.width, height: screenSize.height-40))
myTableView.bounces = false
myTableView.tableFooterView = UIView()
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
myTableView.register(MyTableViewCell.self, forCellReuseIdentifier: "cellID2")
myTableView.dataSource = self
myTableView.delegate = self
for show all part of tableView in screen i have to change the first line above code to this:
let myTableView = UITableView(frame: CGRect(x: 0, y: 40, width: screenSize.width, height: screenSize.height-90))
why this is happening and what should i do to solve this problem thanks
Try something like this but don't forget to add myTableView as a subview before you set anchors:
myTableView.translatesAutoresizingMaskIntoConstraints = false
myTableView.leftAnchor.constraint(equalTo: myTableView.superview!.leftAnchor).isActive = true
myTableView.rightAnchor.constraint(equalTo: myTableView.superview!.rightAnchor).isActive = true
myTableView.topAnchor.constraint(equalTo: myTableView.superview!.topAnchor).isActive = true
myTableView.bottomAnchor.constraint(equalTo: myTableView.superview!.bottomAnchor).isActive = true

Swift UITextField icon position set

Add Icon in UITextField using
var leftImageView = UIImageView()
leftImageView.image = leftImage
textField.leftView = leftImageView
textField.leftViewMode = UITextFieldViewMode.Always
leftImageView.frame = CGRectMake(15, 10, 15, 20)
textField.addSubview(leftImageView)
o/p for this
I found solution like remove this code from above code
textField.leftView = leftImageView
It give icon alignment proper but whenever start editing text field text on icon like this
I want o/p like this
First of all, you should definitely not add the image as a subview to the label. It's enough to set the leftView property.
textField.addSubview(leftImageView) // Delete this line
Secondly, any x or y offsets that you apply to the left view's frame are ignored. The text field will only care about the view's size. If you want to add padding around the image, one option is to use a container view and position the image view inside of it.
let leftImageView = UIImageView()
leftImageView.image = leftImage
let leftView = UIView()
leftView.addSubview(leftImageView)
leftView.frame = CGRectMake(0, 0, 30, 20)
leftImageView.frame = CGRectMake(0, 0, 15, 20)
textField.leftView = leftView
Another option would be to subclass UITextField and override leftViewRectForBounds.
Referring #hennes syntax , there are some syntax missing in swift like CGRect syntax is changed and userNameTextField.leftViewMode = .always is missing
With swift syntax this worked for me :
let leftImageView = UIImageView()
leftImageView.image = UIImage(named: "email")
let leftView = UIView()
leftView.addSubview(leftImageView)
leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
leftImageView.frame = CGRect(x: 10, y: 10, width: 20, height: 20)
userNameTextField.leftViewMode = .always
userNameTextField.leftView = leftView
Try this. May be help you.
var padding: Float = 20
var envelopeView: UIImageView = UIImageView(frame: CGRectMake(padding, 0, 30, 30))
envelopeView.image = UIImage.imageNamed("comment-128.png")
envelopeView.contentMode = UIViewContentModeScaleAspectFit
var viewLeft: UIView = UIView(frame: CGRectMake(padding, 0, 30, 30))
viewLeft.addSubview(envelopeView)
textField.leftView.setFrame(envelopeView.frame)
textField.leftView = viewLeft
textField.leftViewMode = UITextFieldViewModeAlways
var viewRight: UIView = UIView(frame: CGRectMake(textField.frame.size.width - (textField.frame.size.width + 30 + padding), 0, 30, 30))
viewRight.addSubview(envelopeView)
textField.rightView.setFrame(envelopeView.frame)
textField.rightView = viewRight
textField.rightViewMode = UITextFieldViewModeAlways
Swift 3.1
extension UITextField
{
enum Direction
{
case Left
case Right
}
func AddImage(direction:Direction,imageName:String,Frame:CGRect,backgroundColor:UIColor)
{
let View = UIView(frame: Frame)
View.backgroundColor = backgroundColor
let imageView = UIImageView(frame: Frame)
imageView.contentMode = .center
imageView.image = UIImage(named: imageName)
View.addSubview(imageView)
if Direction.Left == direction
{
self.leftViewMode = .always
self.leftView = View
}
else
{
self.rightViewMode = .always
self.rightView = View
}
}
}

swift/ios8: mimick action Sheet behaviour

As it is highly discouraged to use the Action Sheet for overlay controls I am currently trying to mimick it with my own subview but somehow the containing components wont be displayed?
code:
#IBAction func showActionSheet(sender: AnyObject) {
println("SHOW STUFF")
let newView:UIView = UIView()
let myColor:UIColor = UIColor(red:255.0, green:255.0, blue:255.0, alpha:0.6)
newView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
newView.backgroundColor = myColor
view.addSubview(newView)
var pickerFrame: CGRect = CGRectMake(0, self.view.frame.height-(self.view.frame.height+self.view.frame.height-200), self.view.frame.width-20, self.view.frame.height/2)
var picker: UIDatePicker = UIDatePicker(frame: pickerFrame);
picker.backgroundColor = UIColor.whiteColor()
view.addSubview(picker)
var okButton = UIButton()
okButton.titleLabel?.text = "Ok"
okButton.backgroundColor = UIColor.whiteColor()
okButton.frame = CGRectMake(0 , self.view.frame.width-160, self.view.frame.width, 80)
var cancelButton = UIButton()
cancelButton.titleLabel?.text = "Abbrechen"
cancelButton.backgroundColor = UIColor.whiteColor()
cancelButton.frame = CGRectMake(0 , self.view.frame.width-80, self.view.frame.width, 800)
view.addSubview(okButton)
view.addSubview(cancelButton)
let secondView:UIView = UIView()
secondView.frame = CGRectMake(0, 200, self.view.frame.width, self.view.frame.height)
secondView.backgroundColor = UIColor.grayColor()
view.addSubview(secondView)
}
the secondView was just for testing purposes - it will get drawn normally, but somehow my controls wont be
You are not adding the controls to newView the view you placed over the old view. Also the buttons are too big and white. Try this code. This will make them visible. Adjust their height to match the frame of the view and then set the color back. I just changed the background colors to get an idea of the frame. Also call UIButton.setTitle(:forState:) to set the title of the buttons.
#IBAction func showActionSheet(sender: AnyObject) {
println("SHOW STUFF")
let newView:UIView = UIView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
let myColor:UIColor = UIColor(red:255.0, green:255.0, blue:0, alpha:0.6)
newView.backgroundColor = myColor
view.addSubview(newView)
var pickerFrame: CGRect = CGRectMake(0, 0, self.view.frame.width-20, 200)
var picker: UIDatePicker = UIDatePicker(frame: pickerFrame);
picker.backgroundColor = UIColor.redColor()
newView.addSubview(picker)
var okButton = UIButton()
okButton.setTitle("Ok", forState: UIControlState.Normal)
okButton.setTitle("Ok", forState: UIControlState.Highlighted)
okButton.backgroundColor = UIColor.greenColor()
okButton.frame = CGRectMake(0 , self.view.frame.width-160, self.view.frame.width, 80)
var cancelButton = UIButton()
cancelButton.setTitle("Abbrechen", forState: UIControlState.Normal)
cancelButton.setTitle("Abbrechen", forState: UIControlState.Highlighted)
cancelButton.backgroundColor = UIColor.blueColor()
cancelButton.frame = CGRectMake(0 , self.view.frame.width-80, self.view.frame.width, 200)
newView.addSubview(okButton)
newView.addSubview(cancelButton)
}