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
Related
these several days im learning iOS Development, I have a problem where my text button is clipped to the wrong width,
how can I make the UIButton match the width of the content? below is my code
let btnPost = UIButton()
btnPost.setTitle("POST", for: .normal)
btnPost.backgroundColor = Color.shared.accent
btnPost.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
btnPost.layer.cornerRadius = 10
btnPost.layer.masksToBounds = true
btnPost.titleEdgeInsets = UIEdgeInsets(top: 5, left: 15, bottom: 5, right: 15)
btnPost.addTarget(
self,
action: #selector(baseDidTapPost(_:)),
for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: btnPost)
navigationItem.rightBarButtonItem = barButtonItem
Thanks
You have to just remove the btnPost.titleEdgeInsets and add a frame to your button
btnPost = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 20))
complete code :
let btnPost = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 20))
btnPost.setTitle("POST", for: .normal)
btnPost.backgroundColor = UIColor.blue
btnPost.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
btnPost.layer.cornerRadius = 10
btnPost.layer.masksToBounds = true
btnPost.addTarget(self,action: #selector(baseDidTapPost(_:)),for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: btnPost)
navigationItem.rightBarButtonItem = barButtonItem
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)
}
I would like to use a custom View as UIBarButtonItem left in an UINavigationController, is it possible to remove the left padding here?
Ill alrady tried:
let btn = UIButton()
btn.frame = CGRect(x: -50, y: -50, width: 44, height: 50)
btn.setImage(UIImage(name:"img").withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
let leftButtonBar = UIBarButtonItem(customView: btn)
self.navigationItem.leftBarButtonItems = ( [leftButtonBar , otherBtn ])
how to remove space left setting icon
update. tired this code:
let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height:
view.backgroundColor = .gray
let btn = UIButton()
btn.frame = CGRect(x: -15, y: 0, width: 44, height: 50)
btn.setImage(UIImage(name:"img").withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(DashboardTabBarController.openSetting), for: .touchUpInside)
view.addSubview(btn)
let leftButtonBar = UIBarButtonItem(customView: view)
problem this user when clicked btn setting user
and other problem title navigation not center align
You should have noticed that whatever x value you provide to your custom view i.e, btn, it will always start from the same position because internally navigation bar doesn't count for position provided for that item and it always set the internally defined position as in below image, I set the background color to the custom button for better visuals.
So, the trick here is to put your custom button into another container view as below,
let view = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 200, height: 50)))
view.backgroundColor = .green
let btn = UIButton(frame: CGRect(x: -10, y: 10 , width: 30, height: 30))
btn.setImage(UIImage(named: "cancelled")?.withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
btn.backgroundColor = .yellow
btn.tintColor = .red
view.addSubview(btn)
let btn2 = UIButton(frame: CGRect(x: 20, y: 10, width: 30, height: 30))
btn2.setImage(UIImage(named: "cancelled")?.withRenderingMode(.alwaysTemplate), for: .normal)
btn2.contentMode = .scaleAspectFit
btn2.tintColor = .yellow
btn2.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
view.addSubview(btn2)
let itemsContainer = UIBarButtonItem(customView: view)
self.navigationItem.leftBarButtonItems = [itemsContainer]
This will result into this,
Now, if you change the x, y, width, height of btn, it will adjust accordingly.
Please try this. It works fine for me. If this code not working properly then I will give you another solution.
let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
let btn = UIButton()
btn.frame = CGRect(x: -15, y: 0, width: 44, height: 50)
btn.setImage(UIImage(name:"img").withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
view.addSubview(btn)
let leftButtonBar = UIBarButtonItem(customView: view)
self.navigationItem.leftBarButtonItems = ( [leftButtonBar])
Please add other button in View and set the width of view accordingy.It may hepls to you.Thank you
You can set imageInsets and set image this is example of left margin to set back image on position of default back button
let rightBtn = UIBarButtonItem(image: UIImage(named: "BackArrow"), style: .done, target: self, action: #selector(actnClose))
rightBtn.imageInsets = UIEdgeInsets(top: 0, left: -12, bottom: 0, right: 0)
self.navigationItem.setLeftBarButton(rightBtn, animated: true)
Set the x value of cRect to zero:
btn.frame = CGRect(x: 0, y: -50, width: 44, height: 50)
The less painful and actually cleanest solution was to hide the left button and just add a subview directly to the navigationBar and customise it's position and size with the good old constraints. Worked like a charm. Haven't tested it in every situation but seems to work fine.
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 –
I have a view in which i have multiple labels and a button i want the button to be bound to the top right corner
when i apply the add Missing constraints layout the button gets separate position for different devices Please help me to resolve this
NEVER use add missing constraints. This is a bad way of constructing your layout.
The easiest way to get your desired look is to decide on the size you want your button to be (eg. 30x30)
Then you add the following constraints to your button:
This sets the button's width & Height:
Then pin it to the right top corner:
//Just in case if anybody needs it with code:
//I have added 4 buttons to a custom view's each corner:
var customView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
customView.frame = CGRect.init(x: 0, y: 0, width: 200, height: 200)
customView.backgroundColor = UIColor.red //give color to the view
customView.center = self.view.center
let crossButton = UIButton(frame: CGRect(x: -10, y: -10, width: 30, height: 30))
crossButton.layer.cornerRadius = 15
crossButton.backgroundColor = UIColor.black
crossButton.setImage(UIImage(named: "cross.png"), for: .normal)
crossButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
crossButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
let flipButton = UIButton(frame: CGRect(x: customView.frame.width-15, y: -10, width: 30, height: 30))
flipButton.layer.cornerRadius = 15
flipButton.backgroundColor = UIColor.black
flipButton.setImage(UIImage(named: "done.png"), for: .normal)
flipButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
flipButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
let scaleButton = UIButton(frame: CGRect(x: -10, y: customView.frame.height-20, width: 30, height: 30))
scaleButton.layer.cornerRadius = 15
scaleButton.backgroundColor = UIColor.black
scaleButton.setImage(UIImage(named: "done.png"), for: .normal)
scaleButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
scaleButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
let editButton = UIButton(frame: CGRect(x: customView.frame.width-20, y: customView.frame.height-20, width: 30, height: 30))
editButton.layer.cornerRadius = 15
editButton.backgroundColor = UIColor.black
editButton.setImage(UIImage(named: "done.png"), for: .normal)
editButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
editButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
customView.addSubview(crossButton)
customView.addSubview(flipButton)
customView.addSubview(scaleButton)
customView.addSubview(editButton)
self.view.addSubview(customView)
}
func crossButtonTapped(_ sender:UIButton) {
print("Cross Button was tapped")
}