How to change button size? - ios

I have button in my ViewController in storyboard. And I add some constraints to my button in storyboard. I want to change button size in code. But my code doesn’t work. How to fix it?
#IBOutlet var font: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
font = UILabel(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(50), height: CGFloat(20)))
}

If you connect you button from code with Interface Builder and set up constraints, you can change the size of button by changing constraints' constant.
class ViewController: UIViewController {
#IBOutlet var button: UIButton!
#IBOutlet var heightConstaint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
}
func foo() {
heightConstaint.constant = 50.0
view.setNeedsLayout()
}
}
If you don't use constraints, you can just change frame of view.
button.frame = CGRect(x: 0, y: 0, width: 50, height: 100)

You can update the button constraints, update from the code, or from the storyboard to take out the constraints, modify it

First you create button constraints Outlet . then modify the constraints like this:
self.buttonWidthConstaint.constant = 100.0

Related

Size of image inside textfield is not resizing

I have a custom text field and i want to place image on its left side, but when ever i am running the app, the size of image is not adjusting , i.e its full scale and not taking the width and height being provided. The code and pictures are attached
ViewController class:(In which text field is present)
import UIKit
class Signup2ViewController: UIViewController {
#IBOutlet weak var Email: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
Email.leftViewMode = .always
let imageview = UIImageView()
imageview.frame = CGRect(x: 0, y: 0, width: 5.0, height: 5.0)
view.addSubview(imageview)
let icon = UIImage(named: "c.png")
imageview.image = icon
Email.leftView = imageview
// Do any additional setup after loading the view.
}
}
To set the frame for your imageView you need to subclass UITextField and override leftViewRect(forBounds:). The code below will result in a 20x20 view offset 10 points from the left and centered vertically.
class AwesomeTextField: UITextField {
override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
let leftViewHeight: CGFloat = 20
let y = bounds.size.height / 2 - leftViewHeight / 2
return .init(x: 10, y: y, width: leftViewHeight, height: leftViewHeight)
}
}
To add an imageView to the textField you would do this:
class ViewController: UIViewController {
#IBOutlet weak var textField: AwesomeTextField!
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView()
imageView.image = UIImage(named: "c")
textField.leftView = imageView
textField.leftViewMode = .always
}
}
Make sure you set the appropriate class name for the textField in the identity inspector of your storyboard.

Add button overlay on UITableViewController with use static cells

I try to add button overlay on UITableViewController with static cells. But i get this result, button is working, but i not see result of search:
I'm trying to get this result:
I want to button was always at the bottom regardless of scrolling up or down.
In my code i use framework InstantSearch:
import UIKit
import InstantSearch
import WARangeSlider
class SearchTableViewController: UITableViewController {
#IBOutlet weak var resultButton: StatsButtonWidget!
override func viewDidLoad() {
super.viewDidLoad()
resultButton.frame = CGRect(x: 0, y: 0, width: 150, height: 60)
navigationController?.view.addSubview(resultButton)
InstantSearch.shared.registerAllWidgets(in: self.view)
LayoutHelpers.setupResultButton(button: resultButton)
resultButton.addTarget(self, action: #selector(resultButtonClicked), for: .touchUpInside)
}
}
How can i add button overlay on bottom in UITableViewController? Me need use only UITableViewController, not UIViewController with TableView.
You could directly add the button to the UITableView without AutoLayout, and make sure TableView's delegate is the controller, like:
override func viewDidLoad() {
super.viewDidLoad()
self.button.frame = CGRect(x: 0, y: self.tableView.frame.size.height - 50, width: self.tableView.frame.width, height: 50)
self.tableView.addSubview(self.button)
self.tableView.delegate = self
}
Then you are able to fix the button's position by UIScrollView delegate (UITableViewDelegate inherited from this) while TableView is scrolling:
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView == self.tableView) {
let originY = scrollView.frame.size.height - self.button.frame.size.height + scrollView.contentOffset.y
self.button.frame = CGRect(x: 0, y: originY, width: scrollView.frame.width, height: self.button.frame.size.height)
}
}
Alternatively, if you want to position the button by AutoLayout, just define a NSLayoutConstraint property, and bind it to button's bottom space constraint to its super view. Then adjust the constraint's constant value by same mechanism in scrollViewDidScroll function.
You can just add an view at the bottom of your tableview.
override func viewDidLoad() {
super.viewDidLoad()
addResultButtonView()
}
private func addResultButtonView() {
let resultButton = UIButton()
resultButton.backgroundColor = .red
resultButton.setTitle("Hello", for: .normal)
tableView.addSubview(resultButton)
// set position
resultButton.translatesAutoresizingMaskIntoConstraints = false
resultButton.leftAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.leftAnchor).isActive = true
resultButton.rightAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.rightAnchor).isActive = true
resultButton.bottomAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.bottomAnchor).isActive = true
resultButton.widthAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.widthAnchor).isActive = true
resultButton.heightAnchor.constraint(equalToConstant: 50).isActive = true // specify the height of the view
}

ScrollView Add SubView in swift 3

I want to try add a view form on scroll view but view don't fully added on scroll view's frame, my code is:
import UIKit
class AddIncomeVC: UIViewController {
#IBOutlet var scrollView: UIScrollView!
#IBOutlet var views: UIView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.contentSize = views.frame.size
scrollView.addSubview(views)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
views.frame = CGRect(x: 0, y: 0, width: scrollView.frame.size.width, height: scrollView.frame.size.height)
}
}
Thanks,
Edit line
views.frame = CGRect(x: 0, y: 0, width: scrollView.frame.size.width, height: scrollView.frame.size.height)
to
views.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)
I get solved my issue with this code:
import UIKit
class AddIncomeVC: UIViewController {
#IBOutlet var scrollView: UIScrollView!
#IBOutlet var views: UIView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.contentSize = views.frame.size
scrollView.addSubview(views)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
views.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
}
}
Thanks all for giving valuable time for my question.
The problem is: At view did load, your constraints are not updated to the new display sizes.
I believe that your problem occurs because you are running the app using a simulator with different size from your storyboard... a bigger size (storyboard using iPhone SE and running at iphone 6 simulator, for example).
At View did Load, your view already exists, its true... but its constraints still have iphone SE sizes.
So, at view did load you are setting the content size (that you want with an iphone6 screen size) equal to storyboard view size (that still an iphone SE screen size).
Your solution works well. After the iOS layout all subviews, the constraints are updated to the new screen sizes, maintaining its proportions.
At viewDidLayoutSubviews the "view" already have the correct constraint values... So, the view has iphone6 screen sizes, and you could set the frame with the right

swift couldn't add button dynamically to a scroll view

this is my code
class ViewController: UIViewController {
#IBOutlet weak var d: UIScrollView!
let numberOfButtons = 50
override func viewDidLoad() {
super.viewDidLoad()
for index in 0..<self.numberOfButtons {
let frame1 = CGRect(x: 20 + index, y: 20 + index, width: 45, height: 45 )
let button = UIButton()
button.frame = frame1
button.titleLabel?.text = "asdfasdf"
self.d.addSubview(button)
}
}
pretty simple, when i run the simulation, i can't see any button in my scroll view.
i am doing this because i need just to have a scroll view that has more items than the simulate can have, just to check the scroll thing
You just need to set the background color (or the text color), right now they're both white, so it looks like they aren't being drawn. Your code would look like this:
#IBOutlet weak var d: UIScrollView!
let numberOfButtons = 50
override func viewDidLoad() {
super.viewDidLoad()
for index in 0..<self.numberOfButtons {
let frame1 = CGRect(x: 20, y: 20 + (index * 45), width: 45, height: 45 )
let button = UIButton(frame: frame1)
button.setTitle("asdfasdf", forState: .Normal)
button.backgroundColor = UIColor.blackColor()
self.d.addSubview(button)
}
}
I've also changed the frames a bit so they'll draw in a straight line down the scroll view. Also, your view will not scroll unless you set the contentSize correctly, so you'll need to throw in a line immediately after the for-loop like this: self.d.contentSize.height = CGFloat(45*numButtons)

Changing frame of UIView through IBOutlet from storyboard

#IBOutlet weak var outletView: UIView! //Referencing Outlet connected
override func viewDidLoad() {
super.viewDidLoad()
//not working
outletView.frame = CGRectMake(100, 100, 100, 100)
//working
var view = UIView();
view.frame = CGRectMake(0, 20, 100, 100);
self.view.addSubview(view);
}
How can I edit frame of #IBoutlet view as normal UIView instance?
I solved it to remove Use Size Classes on StoryBoard inspector.
When you set the frame in ViewDidload method , it first assigns the frame that you set inViewDidload and then the Storyboard Constraints are applied so your constraints are over-ridden.
In order to change the constraints apply the constraints in ViewDidAppear method and it will be reflected
override func viewDidAppear(animated: Bool)
{
outletView.frame = CGRect(x: 100, y: 100, width: 200, height: 400)
}

Resources