Creating a UIButton inside a subView programmatically not working - ios

I'm trying to add a UIButton inside a UIView called containerView. The containerView is showing normally but the UIButton isn't showing up at all. This is my code:
let containerView = UIView()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
self.containerView.frame = CGRect(x: self.view.frame.size.width - 100, y: 200, width: 225, height: 70)
self.containerView.backgroundColor = UIColor.gray
self.containerView.layer.cornerRadius = 20
self.containerView.clipsToBounds = true
self.view.addSubview(self.containerView)
let button1: UIButton = UIButton()
button1.frame = CGRect(x: self.view.frame.size.width - 70, y: 200, width: 35, height: 35)
button1.clipsToBounds = true
button1.setTitle("Tesing Button", for: .normal)
self.containerView.addSubview(button1)
}
Any help? Thanks!

Your button frame is positioned incorrectly. You have set the value of y as 200 which is beyond the size of your container view.

Related

Add UIButton and UIImage in TableView Section header

I can't figure out what is wrong in this code. i want to display button and image in tableview section header
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
let image = UIImageView()
image.frame = CGRect(x: view.frame.width - 10 , y: 0, width: 20, height: 20)
image.image = UIImage.init(named: "triangle.png")
let button = UIButton(type: .system)
button.frame = CGRect(x: view.frame.origin.x, y: view.frame.origin.y, width: view.frame.width - 30, height: view.frame.height)
button.setTitle("Open", for: .normal)
button.backgroundColor = .yellow
button.addTarget(self, action: #selector(buttonclick), for: .touchUpInside)
view.addSubview(image)
view.addSubview(button)
return view
}
You shouldn't set imageView and button frame by view frame. Because when viewForHeaderInSection function is being called view frame will be 0.
Instead of view you can use tableview
image.frame = CGRect(x: tableView.frame.width - 20 , y: 0, width: 20, height: 20)
button.frame = CGRect(x: 0, y: 0, width: tableView.frame.width - 30, height: 50)
It is recommended to use autolayout instead of setting frame.
Add below code just after initialising view :
view.frame = CGRect(x: 0 , y: 0, width: tableView.frame.width, height: heightForHeaderInSection)
you forgot to set the view's frame.

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

How to create a button and segue from titleView image

Having a problem with customizing the navigationBar. I have changed the titleView text to an image through coding. Now I want the image to be a clickable button with a segue to the starting page (for example a home button to the main viewController). How do I make the titleView image into a button (or item) and how do I create a segue from it. I suppose it has to be done in code since I haven't found any other means of doing it. If you have a solution, please be thorough - I'm a beginner.
Simply add a custom UIView containing UIButton and UIImageView to the titleView of navigationItem.
Example:
override func viewDidLoad()
{
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: 0, y: 0, width: self.view.frame.width - 80, height: 30))
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(showMainVC), for: .touchUpInside)
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width - 80, height: 30))
imageView.image = UIImage(named: "Image")
let customView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width - 80, height: 30))
customView.addSubview(imageView)
customView.addSubview(button)
self.navigationItem.titleView = customView
}
func showMainVC()
{
//TODO:
}

How do I space out the buttons in an array created?

I want to make a popup menu which is a UIView containing an array of buttons which should be existing side by side. Instead, they are all stacked on top of each other despite my changing the center.x dynamically. This is my code snippet:
func createPopUpView(number: Int) -> UIView{
let popUpView: UIView = UIView()
var buttons: [UIButton] = [UIButton]()
let button: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 32, height:42))
button.backgroundColor = UIColor.black
button.layer.cornerRadius = 5
for i in 0..<number {
buttons.append(button)
buttons[i].setTitle(String(i), for: .normal)
buttons[i].center.x += 32
popUpView.addSubview(buttons[i])
}
return popUpView
}
Thank you.
You need to create a new button each time through the loop. Also, need to supply a frame when you create your popUpView...
Try it like this:
func createPopUpView(number: Int) -> UIView{
let popUpView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: number * 32, height: 42))
for i in 0..<number {
let button: UIButton = UIButton(frame: CGRect(x: i * 32, y: 0, width: 32, height:42))
button.backgroundColor = UIColor.black
button.layer.cornerRadius = 5
button.setTitle(String(i), for: .normal)
popUpView.addSubview(button)
}
return popUpView
}

Resources