How add vertical button list from top right corner programmatically in Swift 5 - ios

I need to add buttons from top right corner of the screen. I have added buttons. it is working fine in iPhone. But when I run to iPad it start on middle right corner of the screen. I have use below code..
let button1 = UIButton(frame: CGRect(x: view.frame.size.width - 56, y: view.frame.size.width - 220, width: 30, height: 30))
self.view.addSubview(button1)
let button12 = UIButton(frame: CGRect(x: view.frame.size.width - 56, y: view.frame.size.width - 150, width: 30, height: 30))
self.view.addSubview(button12)
How to add button from top right corner always both phone or iPad?

Change view.frame.size.width to a static value for y position. Because UIView position calculation starts from Top-Left corner.
let button1 = UIButton(frame: CGRect(x: view.frame.size.width - 56, y: 20, width: 30, height: 30))
self.view.addSubview(button1)
let button12 = UIButton(frame: CGRect(x: view.frame.size.width - 56, y: 60, width: 30, height: 30))
self.view.addSubview(button12)
If you want to add AutoLayoutConstraint try this
let button1 = UIButton()
button1.setTitle("Button 1", for: .normal)
button1.backgroundColor = .red
button1.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(button1)
let button12 = UIButton()
button12.setTitle("Button 12", for: .normal)
button12.backgroundColor = .red
button12.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(button12)
button1.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
button1.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -20).isActive = true
button1.heightAnchor.constraint(equalToConstant: 30).isActive = true
button1.widthAnchor.constraint(equalToConstant: 100).isActive = true
button12.topAnchor.constraint(equalTo: button1.bottomAnchor, constant: 20).isActive = true
button12.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -20).isActive = true
button12.heightAnchor.constraint(equalToConstant: 30).isActive = true
button12.widthAnchor.constraint(equalToConstant: 100).isActive = true
To know about translatesAutoresizingMaskIntoConstraints please check it - link

I would look into adding these directly to a storyboard. Possibly create these two buttons in a stackview. If you need go programmatically, the main issue with your above example is to use height instead of width for y:
let button1 = UIButton(frame: CGRect(x: self.view.frame.size.width - 56, y: self.view.frame.size.height - 220, width: 30, height: 30))
self.view.addSubview(button1)
let button12 = UIButton(frame: CGRect(x: self.view.frame.size.width - 56, y: self.view.frame.size.height - 150, width: 30, height: 30))
self.view.addSubview(button12)

You have to pin your buttons with constraints to that view like that:
NSLayoutConstraint.activate([
self.button1.topAnchor.constraint(equalTo: self.view.topAnchor, constant: *first button top margin*),
self.button1.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: *first button right margin*),
self.button2.topAnchor.constraint(equalTo: self.button1.bottomAnchor, constant: *second button top margin*),
self.button2.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: *second button right margin*)
])
Do not forget to add subviews like you did self.view.addSubview(button1)
You can also set constraints for width and height.
Apple docs: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html#//apple_ref/doc/uid/TP40010853-CH16-SW1

Related

SF Symbols not loading into UIImageView

I am creating an app for tracking health information, and I would like to use the two arrow images from SF Symbols. Unfortunately, no matter what I try, these symbols will not show up.
I have already tested the code with an image from the Assets folder, which seems to work with no problem in the UIImageViews I have created. It is only when I attempt to use UIImage(systemName: ) that I am having problems.
self.calendarView = UIView(frame: CGRect(x: 0, y: self.getCurrentY(), width: UIScreen.main.bounds.width, height: 60))
self.calendarView?.backgroundColor = .white
self.calendarView?.layer.borderColor = UIColor.black.cgColor
self.calendarView?.layer.borderWidth = 1
self.currentDate = UILabel(frame: CGRect(x: self.calendarView!.bounds.width / 4, y: self.calendarView!.bounds.height / 2 - 20, width: self.calendarView!.bounds.width / 2, height: 40))
self.currentDate!.text = "5/6/20"
self.currentDate!.textColor = .black
self.currentDate!.layer.borderColor = UIColor.black.cgColor
self.currentDate?.layer.borderWidth = 1
self.currentDate?.textAlignment = .center
self.calendarView?.addSubview(currentDate!)
//let testImage = UIImage(named: "logotest.jpg")!
let image1 = UIImage(systemName: "arrow.left")!.withTintColor(.blue)
let image2 = UIImage(systemName: "arrow.right")!.withTintColor(.blue)
self.leftArrow = UIImageView(image: image1)
self.rightArrow = UIImageView(image: image2)
self.leftArrow?.frame = CGRect(x: 10, y: 10, width: 40, height: 40)
self.rightArrow?.frame = CGRect(x: UIScreen.main.bounds.width - 50, y: 10, width: 40, height: 40)
self.leftArrow?.layer.borderWidth = 1
self.rightArrow?.layer.borderWidth = 1
self.calendarView!.addSubview(self.leftArrow!)
self.calendarView!.addSubview(self.rightArrow!)
self.contentView.addSubview(self.calendarView!)
What seems even stranger is that when I enter a breakpoint, the images actually show! I've attached a picture of this as well. Any help I can get with this is appreciated. Thanks!
Give your imageViews tint color will resolve the issue
leftArrow.tintColor = .blue
leftArrow.tintColor = .blue
1) You should be using constraints / auto-layout instead of explicit frames.
2) You should use if let and guard let to avoid all those ! and ?
Try it like this:
class ViewController: UIViewController {
var leftArrow: UIImageView!
var rightArrow: UIImageView!
var calendarView: UIView!
var currentDate: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//self.calendarView = UIView(frame: CGRect(x: 0, y: self.getCurrentY(), width: UIScreen.main.bounds.width, height: 60))
self.calendarView = UIView()
self.calendarView.backgroundColor = .white
self.calendarView.layer.borderColor = UIColor.black.cgColor
self.calendarView.layer.borderWidth = 1
//self.currentDate = UILabel(frame: CGRect(x: self.calendarView!.bounds.width / 4, y: self.calendarView!.bounds.height / 2 - 20, width: self.calendarView!.bounds.width / 2, height: 40))
self.currentDate = UILabel()
self.currentDate.text = "5/6/20"
self.currentDate.textColor = .black
self.currentDate.layer.borderColor = UIColor.black.cgColor
self.currentDate.layer.borderWidth = 1
self.currentDate.textAlignment = .center
//let testImage = UIImage(named: "logotest.jpg")!
guard let imgLeft = UIImage(systemName: "arrow.left"),
let imgRight = UIImage(systemName: "arrow.right") else {
fatalError("Could not create arrow images!")
}
let image1 = imgLeft.withTintColor(.blue)
let image2 = imgRight.withTintColor(.blue)
self.leftArrow = UIImageView(image: image1)
self.rightArrow = UIImageView(image: image2)
//self.leftArrow.frame = CGRect(x: 10, y: 10, width: 40, height: 40)
//self.rightArrow.frame = CGRect(x: UIScreen.main.bounds.width - 50, y: 10, width: 40, height: 40)
self.leftArrow.layer.borderWidth = 1
self.rightArrow.layer.borderWidth = 1
self.calendarView.addSubview(currentDate)
self.calendarView.addSubview(self.leftArrow)
self.calendarView.addSubview(self.rightArrow)
//self.contentView.addSubview(self.calendarView)
self.view.addSubview(self.calendarView)
[calendarView, currentDate, leftArrow, rightArrow].forEach {
$0?.translatesAutoresizingMaskIntoConstraints = false
}
// respect safe area
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
// calendarView to view (safe area) Top / Leading / Trailing
// Height: 60
calendarView.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
calendarView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),
calendarView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 0.0),
calendarView.heightAnchor.constraint(equalToConstant: 60.0),
// currentDate label to calendarView centerX & centerY
// Width: 1/2 of calendarView width
// Height: 40
currentDate.centerXAnchor.constraint(equalTo: calendarView.centerXAnchor),
currentDate.centerYAnchor.constraint(equalTo: calendarView.centerYAnchor),
currentDate.heightAnchor.constraint(equalToConstant: 40.0),
currentDate.widthAnchor.constraint(equalTo: calendarView.widthAnchor, multiplier: 0.5),
// leftArrow to calendarView Leading: 10 Width: 40 Height: 40 centerY
leftArrow.leadingAnchor.constraint(equalTo: calendarView.leadingAnchor, constant: 10.0),
leftArrow.widthAnchor.constraint(equalToConstant: 40.0),
leftArrow.heightAnchor.constraint(equalToConstant: 40.0),
leftArrow.centerYAnchor.constraint(equalTo: calendarView.centerYAnchor),
// rightArrow to calendarView Trailing: -10 Width: 40 Height: 40 centerY
rightArrow.trailingAnchor.constraint(equalTo: calendarView.trailingAnchor, constant: -10.0),
rightArrow.widthAnchor.constraint(equalToConstant: 40.0),
rightArrow.heightAnchor.constraint(equalToConstant: 40.0),
rightArrow.centerYAnchor.constraint(equalTo: calendarView.centerYAnchor),
])
}
}
Output:
and, with auto-layout and constraints, it auto-sizes when needed - such as on device rotation:

How to Add label On the top of the View Swift

Here is what I am trying to do:
The screenshot is taken from iPhone:
Code Below:
//Show HighLightView
let highLightView = UIView(frame: CGRect(x: self.minXHighLightView!, y: 0, width: self.maxXHighLightView!-self.minXHighLightView!, height: self.soundWaveView.frame.height))
highLightView.backgroundColor = UIColor(displayP3Red: 89/256, green: 206/256, blue: 249/256, alpha: 0.4)
self.soundWaveView.addSubview(highLightView)
//Show HighLight Lable
let highLightLabelCount = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
highLightLabelCount.textColor = .black
highLightLabelCount.font = UIFont(name: "Lato-Bold", size: 20)
highLightView.addSubview(highLightLabelCount)
Question: How to set Frame or constraints label setup at top Left Corner?
Any help would be greatly appreciated.
Thanks in advance.
Your constraint should be centered x,y not top left with
//Show HighLightView
let highLightView = UIView(frame: CGRect(x: self.minXHighLightView!, y: 0, width: self.maxXHighLightView!-self.minXHighLightView!, height: self.soundWaveView.frame.height))
highLightView.backgroundColor = UIColor(displayP3Red: 89/256, green: 206/256, blue: 249/256, alpha: 0.4)
self.soundWaveView.addSubview(highLightView)
//Show HighLight Lable
let highLightLabelCount = UILabel()
highLightLabelCount.textColor = .black
highLightLabelCount.font = UIFont(name: "Lato-Bold", size: 20)
highLightView.addSubview(highLightLabelCount)
highLightLabelCount.translatesAutoresizingMaskIntoConstraints = false
self.highLightView.textAlignment = .center
NSLayoutConstraint.activate([
self.highLightLabelCount.centerXAnchor.constraint(equalTo: self.highLightView.centerXAnchor),
self.highLightLabelCount.centerYAnchor.constraint(equalTo: self.highLightView.centerYAnchor)
])
I think this will be useful if you would like to pin label to top left corner of your highlight view.
NSLayoutConstraint.activate([
highLightView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
highLightView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
highLightLabelCount.leadingAnchor.constraint(equalTo: view.leadingAnchor),
highLightLabelCount.bottomAnchor.constraint(equalTo: highLightView.topAnchor))
])

Setting left view to the TextField with its bottom

I want to set the Leftview to the TextField. But I want to align it to the bottom of the Textfield. So that the leftview frame looks align to the TextField actual text. But I am not getting any luck.
here is how I am trying with it.
let btnSelectCountry = UIButton()
btnSelectCountry.setTitle(title, for: UIControlState.normal)
btnSelectCountry.titleEdgeInsets = UIEdgeInsetsMake(0, 0, -10, 0)
btnSelectCountry.backgroundColor = CommonUtils.hexStringToUIColor(hex: "#000000")
btnSelectCountry.frame = CGRect(x: 0, y: CGFloat(0), width: CGFloat(70), height: CGFloat(25))
btnSelectCountry.addTarget(self, action: #selector(self.selectCountryCallingCode), for: .touchUpInside)
etPhoneNum.leftView = btnSelectCountry
etPhoneNum.leftViewMode = .always
etPhoneNum.leftViewRect(forBounds: CGRect(x: 0, y: CGFloat(-100), width: CGFloat(70), height: CGFloat(25)))
But it is showing vertically aligned in the TextField. How can I move LeftView to bottom inside the TextField??
func setTextLeftView(){
txtField.leftView = UIImageView.init(image: image)
txtField.leftView?.frame = CGRect(x: 0, y: 5, width: 20 , height:20)
txtField.leftViewMode = .always
}
try this and if it does't work for you then let me know if would like to solve your problem :)

Custom titleView changes location after push and pop new screen in stack

I'm trying to implement custom titleView for navigation controller with "title" and "description" labels. If I placed this titleView on first VC, it looks good. If I push second VC into navigation stack and pop it, location of the titleView is changed.
titleView's constraints:
titleLabel.translatesAutoresizingMaskIntoConstraints = false
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
descriptionLabel.widthAnchor.constraint(equalTo: titleLabel.widthAnchor).isActive = true
titleLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
descriptionLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 2.0).isActive = true
In viewDidLoad of VC I use follow code to insert titleView:
navigationItem.titleView = BarTitleView()
navigationItem.titleView?.bounds = CGRect(x: 0, y: 0, width: view.bounds.width, height: 44)
navigationItem.titleView?.updateConstraints()
I tried to insert follow lines into viewWillAppear (second VC has different bar buttons and it can be root of problem), but nothing changes
navigationItem.titleView?.bounds = CGRect(x: 0, y: 0, width: view.bounds.width, height: 44)
navigationItem.titleView?.updateConstraints()
How can I fixed this issue?
I have created a Title and Subtitle in the Navigation bar without the need for Interface Builder and constraint modification.
My Function to create the title view looks as follows:
class func setTitle(title:String, subtitle:String) -> UIView {
let titleLabel = UILabel(frame: CGRect(x: 0, y: -8, width: 0, height: 0))
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.white
titleLabel.font = UIFont.boldSystemFont(ofSize: 17)
titleLabel.text = title
titleLabel.sizeToFit()
let subtitleLabel = UILabel(frame: CGRect(x: 0, y: 12, width: 0, height: 0))
subtitleLabel.backgroundColor = UIColor.clear
subtitleLabel.textColor = UIColor.white
subtitleLabel.font = UIFont.systemFont(ofSize: 12)
subtitleLabel.text = subtitle
subtitleLabel.sizeToFit()
// Fix incorrect width bug
if (subtitleLabel.frame.size.width > titleLabel.frame.size.width) {
var titleFrame = titleLabel.frame
titleFrame.size.width = subtitleLabel.frame.size.width
titleLabel.frame = titleFrame
titleLabel.textAlignment = .center
}
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: titleLabel.frame.size.width, height: titleLabel.frame.size.height))
titleView.addSubview(titleLabel)
titleView.addSubview(subtitleLabel)
let widthDiff = subtitleLabel.frame.size.width - titleLabel.frame.size.width
if widthDiff < 0 {
let newX = widthDiff / 2
subtitleLabel.frame.origin.x = abs(newX)
} else {
let newX = widthDiff / 2
titleLabel.frame.origin.x = newX
}
return titleView
}
Then to use the title view in any view controller I just call this line:
self.navigationItem.titleView = Functions.Views.setTitle(title: "Title String", subtitle: "Subtitle String")

Swift:How can i set button at the end of View Programmatically

On the button click i have set UIWebView Programmatically.
if(button.tag = 1)
{
let webV:UIWebView = UIWebView(frame: CGRectMake(10, 80, (UIScreen.mainScreen().bounds.width)-20, (UIScreen.mainScreen().bounds.height)/2))
webView.loadHTMLString(html!, baseURL: nil)
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.backgroundColor = .greenColor()
button.setTitle("Test Button", forState: .Normal)
button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
webView.addSubview(button)
self.view.addSubview(webV)
}
I tried to set the button at the end of webView and i am not able to set. I tried many time still i am in progress. i want to set the button at the end of webView that i have created programmatically.
Use bounds property of the parent view to position your subview.
let bottomMargin = CGFloat(50) //Space between button and bottom of the screen
let buttonSize = CGSize(width: 100, height: 50)
let button = UIButton(frame: CGRect(
x: 0, y: 0, width: buttonSize.width, height: buttonSize.height
))
button.center = CGPoint(x: parentView.bounds.size.width / 2,
y: parentView.bounds.size.height - buttonSize.height / 2 - bottomMargin)
How can I set button at the end of View Programmatically
#IBOutlet weak var bottomView: UIView!
self.bottamView.addSubview(btnTrvalRpt) btnTrvalRpt.translatesAutoresizingMaskIntoConstraints = false btnTrvalRpt.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true btnTrvalRpt.widthAnchor.constraint(equalToConstant: 50).isActive = true btnTrvalRpt.heightAnchor.constraint(equalToConstant: 50).isActive = true btnTrvalRpt.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -20).isActive = true –

Resources