swift how to remove empty space in button created by code? - swift

How to remove empty space in button created by code? tried to play with insets and constraints but had no solution.
example:
let myButton = UIButton()
myButton.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(myButton)
myButton.backgroundColor = .lightGray
myButton.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
myButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
myButton.heightAnchor.constraint(equalToConstant: 50).isActive = true //this should be not hardcoded but flexible
myButton.widthAnchor.constraint(equalToConstant: 200).isActive = true //this should be not hardcoded but flexible
let downImage = UIImage(named: "caring")
myButton.imageView?.backgroundColor = .green
myButton.imageView?.contentMode = .scaleToFill
myButton.setTitle("Medium Text", for: .normal)
myButton.setImage(downImage, for: .normal)
myButton.semanticContentAttribute = .forceRightToLeft
myButton.imageEdgeInsets = .init(top: 0, left: 16, bottom: 0, right: 0)
// myButton.imageView?.translatesAutoresizingMaskIntoConstraints = false
// myButton.imageView?.widthAnchor.constraint(equalToConstant: 24).isActive = true
// myButton.imageView?.heightAnchor.constraint(equalToConstant: 24).isActive = true
// myButton.imageView?.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
// myButton.imageView?.trailingAnchor.constraint(equalTo: myButton.trailingAnchor, constant: 0).isActive = true
// myButton.contentEdgeInsets.right = 0
// myButton.contentEdgeInsets.top = 0
// myButton.contentEdgeInsets.bottom = 0
//
myButton.titleEdgeInsets.left = 16
myButton.titleEdgeInsets.right = 16

You can use below steps for this purpose.
// step 1: Setup the button
let myButton = UIButton()
myButton.translatesAutoresizingMaskIntoConstraints = false
myButton.backgroundColor = .lightGray
myButton.setTitle("Medium Text", for: .normal)
myButton.backgroundColor = .lightGray
// step 2: Set the imageView Of the button
let downImage = UIImage(named: "caring")
myButton.imageView?.backgroundColor = .green
myButton.imageView?.contentMode = .scaleToFill
myButton.setImage(downImage, for: .normal)
myButton.semanticContentAttribute = .forceRightToLeft
// step 3: Here set up the button frames and add the button to the view
myButton.sizeToFit()
self.view.addSubview(myButton)
myButton.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
myButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
There is no need to set the height and the width anchor which you set in your code.

Related

How to make half of the ImageView on top of custom navigationbar in swift

I am using custom navigation bar for all viewcontrollers.. but in one of my viewcontroller i need half of the image on top of navigationbar, how to do that? please do help
like this i need
for design i am using storyboard,
for that i have taken one imageview inside uiview
uiview constraints
top = 0, leading = 0, trailing = 0, height = 100
imageview constraints
top = -40, horizontal center, height, width = 80
now i am getting like this
my code for custom navigation bar: please help me to make imageview on top of navigationbar in one viewcontroller
func updateNavigationBar(with title : String, isTransparent : Bool = false, isBackNeed : Bool = true, isNotificationNeed : Bool = true, isMenuNeed : Bool = true){
self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
if isTransparent{
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.backgroundColor = .clear
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
self.navigationController?.navigationBar.layer.shadowOpacity = 0
self.navigationController?.navigationBar.layer.shadowOffset = CGSize.zero
self.navigationController?.navigationBar.layer.shadowRadius = 0
}else{
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.backgroundColor = CommonColor.navigationColor
self.navigationController?.navigationBar.barTintColor = CommonColor.navigationColor
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.layer.masksToBounds = false
self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
self.navigationController?.navigationBar.layer.shadowOpacity = 0.2
self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 2.0)
self.navigationController?.navigationBar.layer.shadowRadius = 2
}
let btnTitle = UIBarButtonItem(title: title, style: .plain, target: self, action: #selector(backClick))
btnTitle.tintColor = .white
let backImageView = UIImageView.init(image: CommonImage.back?.withRenderingMode(.alwaysTemplate))
backImageView.tintColor = .white
backImageView.contentMode = .scaleAspectFit
backImageView.frame = CGRect(x:0.0,y:0.0, width:20,height:25.0)
backImageView.contentMode = .scaleAspectFit
backImageView.widthAnchor.constraint(equalToConstant: 20).isActive = true
backImageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
backImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(backClick)))
let btnBack = UIBarButtonItem(customView: backImageView)
let menuImageView = UIImageView.init(image: CommonImage.menu?.withRenderingMode(.alwaysTemplate))
menuImageView.tintColor = .white
menuImageView.contentMode = .scaleAspectFit
menuImageView.frame = CGRect(x:0.0,y:0.0, width:25,height:25.0)
menuImageView.contentMode = .scaleAspectFit
menuImageView.widthAnchor.constraint(equalToConstant: 25).isActive = true
menuImageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
menuImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(menuClick)))
let btnMenu = UIBarButtonItem(customView: menuImageView)
let notificatonView = UIView()
notificatonView.frame = CGRect(x:0.0,y:0.0, width:32,height:32.0)
notificatonView.layer.cornerRadius = 8
notificatonView.alpha = 0.5
notificatonView.backgroundColor = .white
notificatonView.clipsToBounds = true
notificatonView.widthAnchor.constraint(equalToConstant: 32).isActive = true
notificatonView.heightAnchor.constraint(equalToConstant: 32).isActive = true
notificatonView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(notificationClick)))
let notificationBtn: BadgeButton = BadgeButton(type: .custom)
notificationBtn.setImage(CommonImage.notification, for: .normal)
notificationBtn.addAction(for: .touchUpInside) {
}
notificationBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
notificationBtn.badgeBackgroundColor = CommonColor.navigationColor
notificationBtn.badgeTextColor = .white
if notificatonView.subviews.filter({$0 is BadgeButton}).count == 0{
notificatonView.addSubview(notificationBtn)
notificationBtn.center = notificatonView.center
}
let notificationBarButton = UIBarButtonItem(customView: notificatonView)
if isBackNeed{
self.navigationItem.setLeftBarButtonItems([btnBack,btnTitle], animated: true)
self.navigationItem.setRightBarButtonItems( isNotificationNeed ? [notificationBarButton,btnMenu] : [notificationBarButton], animated: true)
}else{
self.navigationItem.setLeftBarButtonItems( isMenuNeed ? [btnMenu,btnTitle] : [btnTitle], animated: true)
self.navigationItem.setRightBarButtonItems( isNotificationNeed ? [notificationBarButton] : nil, animated: true)
}
}
how to show half of the imageview on top of navigationbar please do help
You need to create your custom navigation controller so we able to achieve this type of design.
You need using this type of approach

What is wrong with this StackView?

what is wrong with my StackView?
This is the code:
class PushUpViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setUpStackView()
}
func setUpStackView() {
// SetUp StackView:
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.alignment = .center
stackView.distribution = .fillProportionally
stackView.spacing = 50
view.addSubview(stackView)
// SetUp StackView Constraints:
stackView.pin(to: view)
stackView.setCustomSpacing(50, after: PushUpButton)
stackView.setCustomSpacing(100, after: TimeLabel)
// Set Elements to StackView:
stackView.addArrangedSubview(TimeLabel)
stackView.addArrangedSubview(PushUpButton)
stackView.addArrangedSubview(secondStackView)
// SetUp PushUpButton:
PushUpButton.backgroundColor = .white
PushUpButton.setTitle("\(count)", for: .normal)
PushUpButton.setTitleColor(.systemGray, for: .normal)
PushUpButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 70)
PushUpButton.translatesAutoresizingMaskIntoConstraints = false
PushUpButton.heightAnchor.constraint(equalToConstant: 300).isActive = true
PushUpButton.widthAnchor.constraint(equalToConstant: 150).isActive = true
// SetUp TimeLabel
TimeLabel.textAlignment = .center
TimeLabel.text = "\(counter)"
TimeLabel.textColor = .black
TimeLabel.font = .boldSystemFont(ofSize: 30)
self.view.addSubview(TimeLabel)
TimeLabel.translatesAutoresizingMaskIntoConstraints = false
TimeLabel.widthAnchor.constraint(equalToConstant: 300).isActive = true
TimeLabel.heightAnchor.constraint(equalToConstant: 200).isActive = true
// SetUp SecondStackView
secondStackView.translatesAutoresizingMaskIntoConstraints = false
secondStackView.axis = .horizontal
secondStackView.alignment = .center
secondStackView.distribution = .fillEqually
secondStackView.spacing = 20
// SetUp SecondStackView Constrains
secondStackView.heightAnchor.constraint(equalToConstant: 50).isActive = true
secondStackView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -15).isActive = true
// Set Elements:
secondStackView.addArrangedSubview(breakButton)
secondStackView.addArrangedSubview(stopbutton)
//SetUp BreakButton
breakButton.backgroundColor = .lightGray
breakButton.setTitle("Break", for: .normal)
breakButton.setTitle("Start", for: .selected)
breakButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
breakButton.setTitleColor(.white, for: .normal)
breakButton.layer.cornerRadius = 12
breakButton.layer.borderWidth = 1
breakButton.layer.borderColor = UIColor.white.cgColor
// breakButton.addTarget(self, action: #selector(BreakButtonTapped), for: .touchUpInside)
breakButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
breakButton.widthAnchor.constraint(equalToConstant: 150),
breakButton.heightAnchor.constraint(equalToConstant: 50)
])
// SetUp StopButton:
stopbutton.backgroundColor = .systemRed
stopbutton.setTitle("Stop", for: .normal)
stopbutton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
stopbutton.setTitleColor(.white, for: .normal)
stopbutton.layer.cornerRadius = 12
stopbutton.layer.borderWidth = 1
stopbutton.layer.borderColor = UIColor.white.cgColor
// stopbutton.addTarget(self, action: #selector(stopButtonTapped), for: .touchUpInside)
stopbutton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stopbutton.widthAnchor.constraint(equalToConstant: 150),
stopbutton.heightAnchor.constraint(equalToConstant: 50)
])
}
}
And this is how it look:
But it should looks like this:
This is what comes in the console when I am on the StackView VC:
I have no idea what this means or what I should do to solve this problem
I do not understand StackViews... I watched a lot of yt tutorials but they are all the same and did't help me. My biggest problem is the distribution of the StackView: I don't know where the difference is
First tip: forget using .fillProportionally with stack views. It is almost never used ... and when it is used, it's used for very specific reasons.
Second tip: during development, give your UI elements contrasting background colors to make it easy to see the frames at run-time.
Third tip: Use leadingLowerCase for variable and function names... Use LeadingUpperCase for class names.
Fourth tip: group similar code together - such as setting view properties, setting constraints, etc - and include logical comments to make it easier to follow what your code is doing.
Take a look at this:
class PushUpViewController: UIViewController {
let stackView = UIStackView()
let secondStackView = UIStackView()
let pushUpButton = UIButton()
let breakButton = UIButton()
let stopbutton = UIButton()
let timeLabel = UILabel()
var count: Int = 0
var counter: Float = 0.0
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setUpStackView()
}
func setUpStackView() {
// SetUp StackView:
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.alignment = .fill
stackView.distribution = .fill
// SetUp timeLabel
timeLabel.textAlignment = .center
timeLabel.text = "\(counter)"
timeLabel.textColor = .black
timeLabel.font = .boldSystemFont(ofSize: 30)
// SetUp pushUpButton:
pushUpButton.backgroundColor = .white
pushUpButton.setTitle("\(count)", for: .normal)
pushUpButton.setTitleColor(.systemGray, for: .normal)
pushUpButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 70)
// SetUp secondStackView
secondStackView.axis = .horizontal
secondStackView.alignment = .fill
secondStackView.distribution = .fillEqually
secondStackView.spacing = 20
//SetUp breakButton
breakButton.backgroundColor = .lightGray
breakButton.setTitle("Break", for: .normal)
breakButton.setTitle("Start", for: .selected)
breakButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
breakButton.setTitleColor(.white, for: .normal)
breakButton.layer.cornerRadius = 12
breakButton.layer.borderWidth = 1
breakButton.layer.borderColor = UIColor.white.cgColor
// SetUp stopButton:
stopbutton.backgroundColor = .systemRed
stopbutton.setTitle("Stop", for: .normal)
stopbutton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
stopbutton.setTitleColor(.white, for: .normal)
stopbutton.layer.cornerRadius = 12
stopbutton.layer.borderWidth = 1
stopbutton.layer.borderColor = UIColor.white.cgColor
// add buttons to horizontal second stack view
secondStackView.addArrangedSubview(breakButton)
secondStackView.addArrangedSubview(stopbutton)
// if we want the center PushUpButton to be 300 x 150
// and centered vertically
// we need to embed it in a clear view
let holderView = UIView()
// add PushUpButton to holderView
holderView.addSubview(pushUpButton)
// views added as arrangedSubviews of a stack view automatically get
// .translatesAutoresizingMaskIntoConstraints = false
// but, because we're adding the PushUpButton as a subview
// of holderView, we need to set it here
pushUpButton.translatesAutoresizingMaskIntoConstraints = false
// add views to stack view
stackView.addArrangedSubview(timeLabel)
stackView.addArrangedSubview(holderView)
stackView.addArrangedSubview(secondStackView)
// add stackView to view
view.addSubview(stackView)
// SetUp StackView Constraints:
//stackView.pin(to: view)
// respect safe-area
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
// constrain stackview to full view (safe-area)
// to bottom with Zero extra space
stackView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
// to top with 20-pts "padding"
stackView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
// and 8-pts "padding" on each side
stackView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 8.0),
stackView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -8.0),
// pushUpButton should be 300x150
pushUpButton.widthAnchor.constraint(equalToConstant: 300.0),
pushUpButton.heightAnchor.constraint(equalToConstant: 150.0),
// pushUpButton should be centered in holderView
pushUpButton.centerXAnchor.constraint(equalTo: holderView.centerXAnchor),
pushUpButton.centerYAnchor.constraint(equalTo: holderView.centerYAnchor),
// bottom buttons should have Height: 50
secondStackView.heightAnchor.constraint(equalToConstant: 50.0),
])
// break and stop button actions
//breakButton.addTarget(self, action: #selector(BreakButtonTapped), for: .touchUpInside)
//stopbutton.addTarget(self, action: #selector(stopButtonTapped), for: .touchUpInside)
// during development, so we can see the layout easily
//holderView.backgroundColor = .yellow
//PushUpButton.backgroundColor = .green
//TimeLabel.backgroundColor = .cyan
}
}
Result on iPhone 11:
on iPhone 8:
and with background colors to help during development:
Additional Tip:
When learning auto layout (particularly stack views), work on your layout in Storyboard / Interface Builder. You can immediately see how it looks and what happens when changing values / properties. You can also change the View as: to immediately see how it looks on different devices / screen sizes. If you want to keep everything in code, once you have your layout looking the way you want, then replicate those constraints and settings in code.

Cannot change label position in stackview swift

I want to change X position of a label inside a stackview, there is a button inside the stackview as well. However, I am not able to change the label's position as once I set constraints for the label, errors jump out and want me to delete the constraints.
This is an example of a stackview with a button and a label with changes to the label's x position.
import UIKit
class ViewController: UIViewController {
let stackView = UIStackView()
override func viewDidLoad() {
super.viewDidLoad()
stackView.axis = .vertical
stackView.distribution = .equalSpacing
stackView.alignment = .center
view.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
stackView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
stackView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
let button = UIButton()
button.setTitle("Button", for: .normal)
button.setTitleColor(UIColor.black, for: .normal)
button.backgroundColor = UIColor.lightGray
stackView.addArrangedSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.leftAnchor.constraint(equalTo: stackView.leftAnchor).isActive = true
button.rightAnchor.constraint(equalTo: stackView.rightAnchor).isActive = true
let label = UILabel()
label.text = "Label"
label.textColor = UIColor.black
label.backgroundColor = UIColor.lightGray
stackView.addArrangedSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
// Modify left/right anchor constraints for label x position
label.leftAnchor.constraint(equalTo: stackView.leftAnchor, constant: 24).isActive = true
label.rightAnchor.constraint(equalTo: stackView.rightAnchor).isActive = true
}
}

add gesture recognizer uivew navigation bar swift not working

I have a navigation controller with a navigation bar, I have added a UIView with subview of imageView and UILabel for titleView.
I need to be able to click on that view to do something else with addGestureRecognizer on that view but nothing is printed on the console.
The UIImageView has to be next to the UILabel
Here is the code I tried so far
private func setupNavBarWithUser() {
let titleView = UIView()
let width = titleView.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).width
titleView.frame = CGRect(origin:CGPoint.zero, size:CGSize(width: width, height: 500))
let profileImageView = UIImageView()
profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.contentMode = .scaleAspectFill
profileImageView.layer.cornerRadius = 20
profileImageView.clipsToBounds = true
ImageService.getImage(withURL: NSURL(string: (user?.pictureURL)!)! as URL) { (image) in
profileImageView.image = image
}
titleView.addSubview(profileImageView)
profileImageView.leftAnchor.constraint(equalTo: titleView.leftAnchor).isActive = true
profileImageView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
let nameLabel = UILabel()
titleView.addSubview(nameLabel)
nameLabel.text = user?.first_name
nameLabel.textColor = .white
nameLabel.translatesAutoresizingMaskIntoConstraints = false
nameLabel.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
nameLabel.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true
nameLabel.rightAnchor.constraint(equalTo: titleView.rightAnchor).isActive = true
nameLabel.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = true
titleView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
titleView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true
self.navigationItem.titleView = titleView
let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.testing))
titleView.isUserInteractionEnabled = true
titleView.addGestureRecognizer(recognizer)
}
#objc func testing() {
print("hello")
}
Hope someone can help me with this problem, much thank you !!
UPDATE this is where I need to add a gesture recognizer
Add this method in viewDidLoad/viewWillAppear
if let navigationBar = self.navigationController?.navigationBar {
// create a button
let button = UIButton(type: .custom)
button.frame = CGRect(x: navigationBar.frame.width/2-20, y: 0, width: 100, height: 40)
button.setTitleColor(.red, for: .normal)
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(self.testing), for: .touchUpInside)
// create a imageview
let profileImageView = UIImageView()
profileImageView.contentMode = .scaleAspectFill
profileImageView.layer.cornerRadius = 20
profileImageView.clipsToBounds = true
profileImageView.frame = CGRect(x: navigationBar.frame.width/2-60, y: 0, width: 40, height: 40)
profileImageView.image = UIImage(named: "heart")
// Add two elements to navigationbar
navigationBar.addSubview(profileImageView)
navigationBar.addSubview(button)
}

Swift 3 - Constrain Button Width

I am wanting to create a simple bar at the top of my view, with two buttons side by side, taking up 50% each.
I have created the bar like so:
let topTabView = UIView()
topTabView.backgroundColor = UIColor.red
topTabView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(topTabView)
topTabView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
topTabView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
topTabView.heightAnchor.constraint(equalToConstant: 60).isActive = true
topTabView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
And this works.
I then add my two buttons, and I get all the constraints right, except I'm not sure how to get the width anchor to work, so that each button takes up 50% of the view.
let filterButton = UIButton()
filterButton.backgroundColor = UIColor.red
filterButton.setTitle("Filter", for: .normal)
filterButton.translatesAutoresizingMaskIntoConstraints = false
topTabView.addSubview(filterButton)
filterButton.leftAnchor.constraint(equalTo: topTabView.leftAnchor).isActive = true
filterButton.centerYAnchor.constraint(equalTo: topTabView.centerYAnchor).isActive = true
filterButton.heightAnchor.constraint(equalTo: topTabView.heightAnchor).isActive = true
// NOT SURE ABOUT THIS ONE
filterButton.widthAnchor.constraint(equalToConstant: 200).isActive = true
let mapButton = UIButton()
mapButton.backgroundColor = UIColor.red
mapButton.setTitle("Map", for: .normal)
mapButton.translatesAutoresizingMaskIntoConstraints = false
topTabView.addSubview(mapButton)
mapButton.rightAnchor.constraint(equalTo: topTabView.rightAnchor).isActive = true
mapButton.centerYAnchor.constraint(equalTo: topTabView.centerYAnchor).isActive = true
mapButton.heightAnchor.constraint(equalTo: topTabView.heightAnchor).isActive = true
// NOT SURE ABOUT THIS ONE
mapButton.widthAnchor.constraint(equalToConstant: 200).isActive = true
I tried something like this but it didn't work:
mapButton.widthAnchor.constraint(equalToConstant: toTabView.frame.width / 2.0).isActive = true
Any help would be amazing!
so that each button takes up 50% of the view
That is what the multiplier is for. You aren't using it. Use it!
So, you want a constraint where a button's width is the same as the superview's width except with a 0.5 value for its multiplier.
Example:
let b1 = UIButton()
b1.translatesAutoresizingMaskIntoConstraints = false
b1.backgroundColor = .green
b1.setTitle("Button1", for: .normal)
b1.setTitleColor(.black, for: .normal)
let b2 = UIButton()
b2.translatesAutoresizingMaskIntoConstraints = false
b2.backgroundColor = .yellow
b2.setTitle("Button2", for: .normal)
b2.setTitleColor(.black, for: .normal)
self.view.addSubview(b1)
self.view.addSubview(b2)
NSLayoutConstraint.activate([
b1.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50),
b2.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50),
b1.leadingAnchor.constraint(equalTo:self.view.leadingAnchor),
b2.leadingAnchor.constraint(equalTo:b1.trailingAnchor),
b1.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.5),
b2.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.5),
])
Result: