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)
}
Related
I am trying to add a circle background to an image view. But I only see the image without background.
lazy var circle: UIImageView = {
let view = UIImageView()
view.contentMode = .scaleAspectFit
view.image = UIImage(named: "iconsCancelThin")
view.frame = CGRect(x: 0, y: 0, width: 32, height: 32)
view.tintColor = KSColor.neutral700.getColor()
view.backgroundColor = KSColor.neutral700.getColor()
view.clipsToBounds = true
view.layer.cornerRadius = 16
return view
}()
guard let image = circle.image else { return }
self.cancelButton.setBackgroundImage(image, for: .normal, barMetrics: .default)
I'm new in Swift and trying to create Temperature Converter from ºC to ºF programmatically. But I have no idea how to change meaning of my label when l drag the slider.
I tried to change addTarget, but it didn't help. I really have no idea what's wrong and why my label still being "0ºC".
enter image description here
Here is my code:
class ListVC: UIViewController {
var cLabel : UILabel = UILabel()
var slider : UISlider = UISlider()
override func viewDidLoad() {
super.viewDidLoad()
**// creating label**
let cLabel = UILabel()
cLabel.frame = CGRect(x: 150, y: 250, width: 100, height: 200)
cLabel.textAlignment = .center
cLabel.text = "0ºC"
cLabel.textColor = .black
cLabel.font = UIFont(name: "Apple SD Gothic Neo", size: 25)
self.view.addSubview(cLabel)
self.view = view
**// creating slider**
let slider = UISlider()
slider.frame = CGRect(x: 110, y: 300, width: self.view.frame.width, height: 250)
slider.center = self.view.center
slider.translatesAutoresizingMaskIntoConstraints = false
slider.minimumValue = 0
slider.maximumValue = 100
slider.value = 0
slider.isContinuous = true
slider.isUserInteractionEnabled = true
slider.addTarget(self, action:#selector(sliderValue(sender:)), for: .valueChanged)
self.view.addSubview(slider)
}
**// creating function that had to change label value but something went wrong**
#objc func sliderValue(sender: UISlider) {
cLabel.text = String(sender.value)
}
Thank you!
in my project I have a Navigation Bar with a Button to open a slide menu.
Now I am trying to set a title image instead of the title string. Unlikely the image is pushed slightly to the right instead of being centered. I thought its because of the slide menu button in the left corner of my navigation bar. If I set a title in main.storyboard everything look properly. Why is it so, that my image won't be centered.
Image function:
func addNavBarImage() {
let navController = navigationController!
let image = UIImage(named: "TransparentLogo")
let imageView = UIImageView(image: image)
let bannerWidth = navController.navigationBar.frame.size.width
let bannerHeight = navController.navigationBar.frame.size.height
let bannerX = bannerWidth - image!.size.width
let bannerY = bannerHeight - image!.size.height
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
Button function:
func addSlideMenuButton(){
let btnShowMenu = UIButton(type: UIButton.ButtonType.system)
btnShowMenu.setImage(self.defaultMenuImage(), for: UIControl.State())
btnShowMenu.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnShowMenu.addTarget(self, action: #selector(BaseViewController.onSlideMenuButtonPressed(_:)), for: UIControl.Event.touchUpInside)
btnShowMenu.tintColor = UIColor(red: 3, green: 49, blue: 79)
let customBarItem = UIBarButtonItem(customView: btnShowMenu)
self.navigationItem.leftBarButtonItem = customBarItem;
}
Change the frame of your titleView in func addNavBarImage() like this:-
func addNavBarImage() {
let navController = navigationController!
let image = UIImage(named: "TransparentLogo")
let imageView = UIImageView(image: image)
let bannerWidth = navController.navigationBar.frame.size.width
let bannerHeight = navController.navigationBar.frame.size.height
let bannerX = bannerWidth - image!.size.width
let bannerY = bannerHeight - image!.size.height
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
to this
func addNavBarImage() {
let imageView = UIImageView(image: #imageLiteral(resourceName: "TransparentLogo"))
imageView.frame = CGRect(x: 0, y: 0, width: 170, height: 30)
imageView.contentMode = .scaleAspectFit
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 170, height: 30))
titleView.addSubview(imageView)
titleView.backgroundColor = .clear
self.navigationItem.titleView = titleView
}
func centeredNavBarImageView() {
if let navcontroller = navigationController {
let image = #imageLiteral(resourceName: "logo")
let imageView = UIImageView(image: image)
let bannerWidth = navcontroller.navigationItem.accessibilityFrame.size.width
let bannerHeight = navcontroller.navigationBar.frame.size.height
let bannerX = bannerWidth / 2 - image.size.width / 2
let bannerY = bannerHeight / 2 - image.size.height / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit
self.navigationItem.titleView = imageView
}
Is it possible to place UIStackView in NavigationBar programmaticaly using swift? I want to place there StackView with two arranged stackviews. But when i do that , it shows nothing in navigation bar. If it is possible, please provide example. Thanks
Finally I found solution
let btnSort = UIButton(type: .system)
btnSort.frame = CGRect(x: 0, y: 0, width: 120, height: 40)
btnSort.tintColor = UIColor.white
btnSort.setImage(UIImage(named:"ic_controls_icon.png"), for: .normal)
btnSort.imageEdgeInsets = UIEdgeInsets(top: 6,left: -10,bottom: 6,right: 34)
btnSort.titleEdgeInsets = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 14)
btnSort.setTitle("SORT", for: .normal)
btnSort.layer.borderWidth = 1.0
btnSort.backgroundColor = UIColor.red //--> set the background color and check
btnSort.layer.borderColor = UIColor.white.cgColor
let btnControl = UIButton(type: .system)
btnControl.frame = CGRect(x: 0, y: 0, width: 120, height: 40)
btnControl.tintColor = UIColor.white
btnControl.setImage(UIImage(named:"ic_controls_icon.png"), for: .normal)
btnControl.imageEdgeInsets = UIEdgeInsets(top: 6,left: -10,bottom: 6,right: 34)
btnControl.titleEdgeInsets = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 14)
btnControl.setTitle("SORT", for: .normal)
btnControl.layer.borderWidth = 1.0
btnControl.backgroundColor = UIColor.red //--> set the background color and check
btnControl.layer.borderColor = UIColor.white.cgColor
let view = UIStackView(frame: CGRect(x: 0, y: 0, width: 300, height: 50))
view.axis = .horizontal
view.distribution = .fillEqually
view.spacing = 5
view.addArrangedSubview(btnSort)
view.addArrangedSubview(btnControl)
let mainTitleView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 50))
mainTitleView.addSubview(view)
navigationItem.titleView = mainTitleView
You can make any UIView subclass (of which UIStackView is one) the navigation bar's title using your view controller's navigationItem.titleView property.
You can test this out in a playground…
import UIKit
import PlaygroundSupport
let vc = UIViewController()
vc.view.backgroundColor = .white
let nav = UINavigationController(rootViewController: vc)
let topLabel = UILabel()
topLabel.font = UIFont.boldSystemFont(ofSize: 20)
topLabel.text = "Hello"
let bottomLabel = UILabel()
bottomLabel.font = UIFont.systemFont(ofSize: 16)
bottomLabel.text = "World!"
let stackView = UIStackView(arrangedSubviews: [topLabel, bottomLabel])
stackView.axis = .vertical
vc.navigationItem.titleView = stackView
PlaygroundPage.current.liveView = nav.view
PlaygroundPage.current.needsIndefiniteExecution = true
var navTitle: String? = "Preview Checklist"
var navSubTitle: String? = "Edit Checklist >"
lazy var titleStackView: UIStackView = {
let titleLabel = UILabel()
titleLabel.textAlignment = .left
titleLabel.text = navTitle
//titleLabel.font = UIFont(name: "RawlineMedium-Regular", size:CGFloat(15))
titleLabel.textColor = .white
let subtitleLabel = UILabel()
subtitleLabel.textAlignment = .left
subtitleLabel.text = navSubTitle
//subtitleLabel.font = UIFont(name: "RawlineMedium-Regular", size:CGFloat(11))
subtitleLabel.textColor = .white
let stackView = UIStackView(arrangedSubviews: [ titleLabel, subtitleLabel])
stackView.axis = .vertical
stackView.backgroundColor = .blue
return stackView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.titleView = titleStackView
}
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
}
}
}