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

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 –

Related

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

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

Add button to right side of screen

I have a UIButton added to the left side of the screen. I have added it like this:
let screenSize: CGRect = UIScreen.main.bounds
let menuButtonSize: CGSize = CGSize(width: 44, height: 44)
func confiureCloseButton() {
let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: menuButtonSize))
button.setImage(UIImage(named: "icon_close"), for: UIControl.State.normal)
button.center = CGPoint(x: 50, y: 61)
button.layer.zPosition = 1
button.addTarget(self, action: #selector(closeButtonAction), for: .touchUpInside)
self.view.addSubview(button)
}
How can I add another button to the right side?
You can try
firstBu.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(firstBu)
secondBu.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(secondBu)
NSLayoutConstraint.activate([
firstBu.leadingAnchor.constraint(equalTo: self.view.leadingAnchor,constant:50),
firstBu.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor,constant:61),
firstBu.widthAnchor.constraint(equalToConstant: 44),
firstBu.heightAnchor.constraint(equalToConstant: 44),
secondBu.leadingAnchor.constraint(equalTo: self.firstBu.trailingAnchor,constant:20),
secondBu.centerYAnchor.constraint(equalTo: self.firstBu.centerYAnchor),
secondBu.widthAnchor.constraint(equalToConstant: 44),
secondBu.heightAnchor.constraint(equalToConstant: 44)
])
Assuming you want the button to be a mirror image of the left button:
func configureRightButton() {
let rightButton = UIButton(frame: CGRect(origin: CGPoint.zero, size: menuButtonSize))
let frameWidth = view.frame.width
rightButton.center = CGPoint(x: frameWidth - 50, y: 61)
rightButton.layer.zPosition = 1
view.addSubview(rightButton)
}

Swift position and constraints programmatically

I have the following view:
I have to place a UIButton or a UILabel below Account balance depending if this movement is rejected or not (just check a variable).
I've created programmatically the button & label depending on that, but I don't know how to add it to that specific place and add constraints.
func checkRejected () {
if !movement.rejected {
let xposition = amountLabel.frame.origin.x
let yposition = balanceLabel.frame.origin.y + 300
let button = UIButton(frame: CGRect(x: xposition, y: yposition, width: 100, height: 50))
button.setTitle("Reject", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.addTarget(self, action: #selector(rejectAction), for: .touchUpInside)
self.view.addSubview(button)
}
else {
let xposition = amountLabel.frame.origin.x
let yposition = balanceLabel.frame.origin.y + 300
let label = UILabel(frame: CGRect(x: xposition, y: yposition, width: 100, height: 50))
label.text = "Rejected"
label.textColor = UIColor.red
self.view.addSubview(label)
}
}
How I could do that?
You can try
self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
button.topAnchor.constraint(equalTo: self.balanceLabel.bottomAnchor,constant:30),
button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
])
Same for the label
Note: Since the button and the label have an intrinsic content size , then we don't have to set width && height

Add buttons to NavigationItem

I try to add button and ImageView on NavigationItem. For this objects I added two functions TouchUp for it. This two objects I added on uiview. UIView I added to UINavigaionItem. I added UIGesture for ImageView and TouchUp action for button. But button and ImageView don't clicking.
The code bellow:
let navView = UIView()
navView.userInteractionEnabled = true
let btn = UIButton(type: UIButtonType.Custom) as UIButton
btn.addTarget(self, action: #selector(ViewController.on_clicked_btn(_:)), forControlEvents: UIControlEvents.TouchUpInside)
btn.frame = CGRectMake(-65, -22, 130, 42)
btn.setTitle("title", forState: .Normal)
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center
btn.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
btn.titleLabel?.backgroundColor = UIColor.clearColor()
photo.frame = CGRect(x: -110, y: -18, width: 33, height: 33)
photo.layer.borderWidth = 0.5
photo.layer.masksToBounds = false
photo.layer.borderColor = UIColor.grayColor().CGColor
photo.layer.cornerRadius = self.photo_avatar.frame.height/2
photo.clipsToBounds = true
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.on_clicked_photo(_:)))
photo.addGestureRecognizer(tapRecognizer)
photo.userInteractionEnabled = true
navView.addSubview(photo)
navView.addSubview(btn)
self.navigationItem.titleView = navView
Any ideas? Thanks for your help.
It is because you were not initialising UIIview rect in:
let navView = UIView()
Replace this with
let navView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 168, height: 40))
Replace bin frame with:
btn.frame = CGRect(x: 38, y: 0, width: 130, height: 42)
and photo frame
photo.frame = CGRect(x: 0, y: 0, width: 33, height: 33)

UISearchBar and Two buttons in the Navigation Controller

I am trying to add two buttons and a SearchBar to my navigation controller but the alignment is not correct. I have tried almost everything such as changing the size of SearchBar, UIButton or changing the Y positioning but it didn't work. Any idea how to fix this issue?
I am using iOS 11 and Xcode 9
searchController = UISearchController(searchResultsController: nil)
searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: 200, height: 30)
searchController?.delegate = self
searchController?.searchResultsUpdater = self
let refineButton = UIButton.init(type: UIButtonType.custom)
refineButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
refineButton.setImage(#imageLiteral(resourceName: "settings-button"), for: UIControlState.normal)
refineButton.widthAnchor.constraint(equalToConstant: 30).isActive = true
refineButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
let refineItem = UIBarButtonItem(customView: refineButton)
navigationItem.leftBarButtonItem = refineItem
navigationItem.titleView = searchController?.searchBar
searchController?.searchBar.sizeToFit()
Please check :
override func viewWillLayoutSubviews() {
searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: 300, height: 30)
searchController?.searchBar.sizeToFit()
}

Resources