Add button to right side of screen - ios

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)
}

Related

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

Two UIButton form one having gradient color and another having border and rounded corner

I am trying to achive this using gradient color and corner radius on [.topLeft,.bottomLeft] and [.topRight,.bottomRight] but I am not able to achieve the border color on one side.
Here is my code
//btnMale gradient color
btnMale.roundCorners([.topLeft,.bottomLeft], radius: 20)
btnFemale.roundCorners([.topRight,.bottomRight], radius: 20)
btnMale.backgroundColor = .clear
let gradient1: CAGradientLayer = CAGradientLayer()
gradient1.frame = CGRect(x: 0, y: 0, width: (self.view.frame.size.width - 90)/2 , height: btnMale.frame.size.height)
gradient1.colors = [Colors.appYellow,Colors.appRed].map { $0.cgColor }
gradient1.startPoint = GradientOrientation.horizontal.startPoint
gradient1.endPoint = GradientOrientation.horizontal.endPoint
btnMale.layer.insertSublayer(gradient1, at: 0)
btnMale.applyGradient(withColours: [Colors.appYellow,Colors.appRed], gradientOrientation: .horizontal)
btnMale.borderWidth = 0.0
btnFemale.borderWidth = 0.5
btnFemale.borderColor = .black
btnMale.setImage(UIImage(named: Images.imgMaleIconWhite), for: .normal)
btnFemale.setImage(UIImage(named: Images.imgFemaleIconBlack), for: .normal)
and here is my output
Please help.
Pradip it's better to add buttons in UIView. It will be easy for you to manage. I made UI like you want using UIView. See this,
let button = UIButton.init(frame: CGRect.init(x: 0, y: 250, width: 200, height: 100))
button.setTitle("HI", for: .normal)
button.addTarget(self, action: #selector(self.btnTapped(pageNo:)), for: .touchUpInside)
self.view.addSubview(button)
let view = UIView.init(frame: CGRect.init(x: 50, y: 250, width: 250, height: 50))
view.backgroundColor = UIColor.red
self.view.addSubview(view)
let btnMale = UIButton.init(type: .custom)
btnMale.frame = CGRect.init(x: 0, y: 0, width: 125, height: 50)
btnMale.setTitle("Male", for: .normal)
btnMale.applyGradient(colours: [UIColor.yellow,UIColor.red])
view.addSubview(btnMale)
let btnFemale = UIButton.init(type: .custom)
btnFemale.frame = CGRect.init(x: 125, y: 0, width: 125, height: 50)
btnFemale.setTitle("Female", for: .normal)
view.addSubview(btnFemale)
view.layer.cornerRadius = 25
view.clipsToBounds = true
Output of above code will look like this.

How to add multiple button in a scrollView programmatically

I am trying to put multiple buttons in a scrollView but it just scrolls background view.
class HomeVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Home"
let scrollView = UIScrollView()
let view = UIView()
scrollView.frame = self.view.bounds
self.view.backgroundColor = .green
scrollView.backgroundColor = .blue
scrollView.addSubview(view)
self.view.addSubview(scrollView)
scrollView.isPagingEnabled = true
scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height * 3)
view.frame = CGRect(x: 0, y: self.view.frame.size.height, width: self.view.frame.size.width, height: self.view.frame.size.height)
view.backgroundColor = .yellow
attractivePlaceButtonSetup()
eatDrinkButtonSetup()
ShoppingButtonSetup()
festivalEventButtonSetup()
hotelGuestHouseButtonSetup()
travellerEssentialButtonSetup()
dealButtonSetup()
seeDoButtonSetup()
}
I wrote this code for button frame
func eatDrinkButtonSetup(){
let button = UIButton()
button.frame = CGRect(x: 5, y: 225, width: self.view.frame.size.width - 10, height: 150)
button.setTitle("Eat & Drink", for: .normal)
button.setBackgroundImage(#imageLiteral(resourceName: "imageName"), for: .normal)
button.titleEdgeInsets = UIEdgeInsets(top: -120, left: -200, bottom: 0, right: 0)
button.addTarget(self, action: #selector(targetEatDrink), for: .touchUpInside)
view.addSubview(button)
}
}
I also try to such way but it just scroll a button.
scrollView.addSubview(attractivePlaceButtonSetup)
self.view.addSubview(scrollView)
//Try this...
//Background Scroll Creation
var stickersScrollViewCount = 0
func stickersScrollContents() {
var xCoord: CGFloat = 5
let yCoord: CGFloat = 5
let buttonWidth:CGFloat = 45.0
let buttonHeight: CGFloat = 45.0
let gapBetweenButtons: CGFloat = 5
for i in 0..<stickersImageArray.count{
stickersScrollViewCount = i
// Button properties
let filterButton = UIButton(type: .custom)
filterButton.frame = CGRect(x: xCoord, y: yCoord, width: buttonWidth, height: buttonHeight)
filterButton.tag = stickersScrollViewCount
filterButton.backgroundColor = UIColor.clear
filterButton.setTitleColor(UIColor.white, for: .normal)
filterButton.titleLabel?.adjustsFontSizeToFitWidth = true
filterButton.showsTouchWhenHighlighted = true
let myimage = UIImage(named: stickersImageArray[stickersScrollViewCount])
filterButton.setImage(myimage, for: .normal)
filterButton.addTarget(self, action:#selector(StickersActionTapped), for: .touchUpInside)
filterButton.layer.cornerRadius = 5
filterButton.clipsToBounds = true
xCoord += buttonWidth + gapBetweenButtons
bgScrollView.addSubview(filterButton)
}
bgScrollView.contentSize = CGSize(width: buttonWidth * CGFloat(stickersScrollViewCount+2), height: yCoord)
}
//Call the function where ever you want viewDidLoad() or on Button Click!!
//Hope this helps!!!

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 –

Bound the button to top right corner of the view iOS swift

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")
}

Resources