Adding UITableView backgroundView with UIView with UIButton - ios

I am trying to set my UITableView backgroundView to UIView and inside the UIView I added a UIButton but the UIButton is not appearing. What am I doing wrong?
let view: UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.menuTable.bounds.size.width, height: self.menuTable.bounds.size.height))
let loginButton: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
loginButton.setTitle("Login", for: .normal)
loginButton.backgroundColor = UIColor.white
loginButton.tintColor = UIColor.black
loginButton.addTarget(self, action:#selector(self.pressedConnection), for: .touchUpInside)
view.addSubview(loginButton)
self.menuTable.backgroundView = view
self.menuTable.separatorStyle = .none

Related

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.

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)

How to add a static header in JSQMessageView Controller

How i can add header just like the image below which should not scroll up with view controller.
To add static header view, use following function (Swift 4):
Note: I have tried to put this kind of header view directly using storyboard. But, it wont worked. So, finally I achieved it by adding programatically.
func addHeaderView() {
let selectableView = UIView(frame: CGRect(x: 0, y: 20, width: self.view.bounds.width, height: 60))
selectableView.backgroundColor = UIColor(red: 56.0/255.0, green: 120.0/255.0, blue: 222.0/255.0, alpha: 1.0)
let btn: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
btn.setImage(UIImage(named:"ArrowBack"), for: .normal)
btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btn.tag = 1
selectableView.addSubview(btn)
let titleLabel = UILabel(frame: CGRect(x: btn.frame.size.width+5, y: 5, width: selectableView.frame.size.width-60, height: 18))
titleLabel.textColor = UIColor.white
titleLabel.text = "Winter event 2017"
selectableView.addSubview(titleLabel)
let subTitleLabel = UILabel(frame: CGRect(x: btn.frame.size.width+5, y: 30, width: 100, height: 16))
subTitleLabel.textColor = UIColor.white
subTitleLabel.text = "135 members"
selectableView.addSubview(subTitleLabel)
let btnOptions: UIButton = UIButton(frame: CGRect(x: selectableView.frame.size.width-60, y: 0, width: 50, height: 50))
btnOptions.setImage(UIImage(named:"Settings"), for: .normal)
btnOptions.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btnOptions.tag = 2
selectableView.addSubview(btnOptions)
view.addSubview(selectableView)
}
//then make a action method :
#objc func buttonAction(sender: UIButton!) {
let btnsendtag: UIButton = sender
if btnsendtag.tag == 1 {
self.navigationController?.popViewController(animated: true)
} else {
askForSettings()
}
}
Also put this line of code in viewDidLoad()
self.collectionView?.collectionViewLayout.sectionInset = UIEdgeInsets(top: 80, left: 0, bottom: 0, right: 0)

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

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