Swift / How to set addSubview to Subview? - ios

I want to add a Subview on my googleAdBanner-Subview. (as a custom close UIButton).
If I add the UIButton as subview to self.view.addSubview(btn), it is working. But since it may take a while to load an Ad, sometimes the UIButton is visible even tho the googleAdBanner is still invisible.
If I add UIButton as subview of googleAdBanner, the UIButton will not be displayed.
override func viewDidLoad() {
super.viewDidLoad()
let googleAdBanner = GADBannerView(frame:CGRectMake(0,self.view.frame.size.height - 50,self.view.frame.size.width,50))
googleAdBanner.adUnitID = "ca-app-pub-xx"
googleAdBanner.rootViewController = self
googleAdBanner.loadRequest(GADRequest())
self.view.addSubview(googleAdBanner)
let btn: UIButton = UIButton(frame: CGRectMake(self.view.frame.size.width - 25, self.view.frame.size.height - 50, 25, 25))
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("Click Me", forState: UIControlState.Normal)
btn.addTarget(self, action: #selector(RootVC.buttonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
btn.tag = 1
googleAdBanner.addSubview(btn)
}
What am I missing? Help is very appreciated.

make your 'btn' sth like: let btn: UIButton = UIButton(frame: CGRectMake(0,0, btnWidth, btnHeight)) after adding that as subview to the banner, then adjust the button frame either by setting the CGrect or auto layout

Related

Circular UIBarButtonItem button image get stretched

Added circular button on self.navigationItem.leftBarButtonItem for specific UIViewController of a TabBarcontroller after switching one tab and getting back to specific tab. The circular button is stretched.
override func viewDidLoad() {
infoButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
infoButton.sd_setBackgroundImage(with: URL(string: "https://www.w3schools.com/howto/img_avatar.png"), for: .normal)
infoButton.addTarget(self, action: #selector(menuButtonPressed), for: .touchUpInside)
infoButton.layer.cornerRadius = infoButton.bounds.size.width / 2
infoButton.clipsToBounds = true
infoButton.layer.borderWidth = 1.0
infoButton.imageView?.contentMode = .scaleToFill
infoButton.layer.borderColor = UIColor.init(hexString: "#AAC8FF").cgColor
let infoItem = UIBarButtonItem(customView: infoButton)
self.navigationItem.leftBarButtonItem = infoItem
}
Error Image:
You forgot to give infoButton a size. You have to use auto layout. Give it a width constraint with a constant 40 and a height constraint with a constant 40.

part of UIButton is not responsive to touch events

I have created a UIView with a subview of type UIButton. The button's size is bigger than the view's. Whenever I touch the button only the part that is the same size as superview is responsive to touch events. I created a drawing of view hierarchy to show you what I mean:
Only the red part of a button is responsive to touch events. Wht is this happening and how can I fix this?
This is the code in viewDidLoad, where I create the button programatically
let button = UIButton(frame: CGRectMake(-8, -8, 52, 52))
moveView.addSubview(button)
button.setBackgroundImage(UIImage(named: "trIcon"), forState: UIControlState.Normal)
button.adjustsImageWhenHighlighted = false
button.addTarget(self, action: "gogo", forControlEvents: UIControlEvents.TouchUpInside)
moveView.clipsToBounds = false
The UIView is created in storyboard.
Please follow the instruction as I tried your coding
let button = UIButton(frame: CGRectMake(0, 0, giveValueLessthanMoveViewWidthSize, giveValueLessthanMoveViewHeightSize)) //Whatever give value less than your view size(x,y,width,height)
button.setBackgroundImage(UIImage(named: "trIcon"), forState: UIControlState.Normal)
button.adjustsImageWhenHighlighted = false
button.backgroundColor = UIColor.blueColor()
button.addTarget(self, action: "gogo:", forControlEvents: UIControlEvents.TouchUpInside)
moveView.clipsToBounds = false
moveView.addSubview(button)
Then button action method
func gogo(sender:UIButton!)
{
println("Button Clicked")
}

How to have buttons scroll with background in Swift?

I have an image that you can scroll through using UIScrollView. I want to add buttons to it. The buttons need to stay attached to the image and scroll with it, not separately. I need this all done programmatically.
This is the code I have right now:
imageView = UIImageView(image: UIImage(named: "Map 1.png"))
// 2
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.blackColor()
// 3
scrollView.contentSize = imageView.bounds.size
// 4
scrollView.addSubview(imageView)
view.addSubview(scrollView)
scrollView.contentSize.height = scrollView.frame.size.height
you just need to add button to scrollview:
var button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Button", forState: UIControlState.Normal)
button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
scrollView.addSubview(button)

Make the navigationbar title clickable swift

I would like to make the navigationbar title to be clickable. When user click on it, it should perform a segue. But I have no idea how to do this.
I have tried the following to get the title and apply the Tap gesture to it.
var subviews = self.navigationController?.navigationBar.subviews
if let subviews = subviews {
// Better check for array length before accessing to the 1st element
var subview = subviews [0]
}
but its giving me error Variable subview inferred to have type AvyObject, which may be unexpected
One more approach to add button as a title of navigation controller.
You need to set navigation item title view to your button object
Create button object in viewDidLoad() method:
Swift 4.0 Edit
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
button.backgroundColor = .red
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(clickOnButton), for: .touchUpInside)
navigationItem.titleView = button
Here you can see in last line button is directly set to the titleView of navigationItem which will add button at the center of navigation bar.
Action method for button is below:
#objc func clickOnButton() {
}
Kampai's answer updated for Swift 3:
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(self.clickOnButton), for: .touchUpInside)
self.navigationItem.titleView = button
To get rid of that error, specify a type for your if let constant. I'd also recommend changing that if let constant name since it's the same as the one already declared in the previous line.
var subviews = self.navigationController?.navigationBar.subviews
if let subviewArray:NSArray = subviews {
// Better check for array length before accessing to the 1st element
var subview:UILabel = subviewArray[0] // <-- If the subview's a UILabel
}

UIButton title not displaying

I just started developing in Swift.
I created an UIButton dynamically and added it to the View.
My issue is the view is not displaying the button.
My Code:
var button:UIButton = UIButton();
button.frame = CGRect(x: 100, y: 250, width: 100, height: 50);
button.setTitle("Midhun", forState: UIControlState.Normal);
self.view.addSubview(button);
Then I added image to the button and then it is showing:
button.setImage(UIImage(named: "step_first.jpg"), forState: UIControlState.Normal);
I couldn't figure out what is happening. Please help
In case you subclass UIButton and override its layoutSubviews(), make sure you call super.layoutSubviews() inside
For me the issue was that I wasn't using the setter methods.
I was doing notificationsButton.titleLabel?.text = "x" instead of notificationsButton.setTitle("x", forState: UIControlState.Normal)
I actually figured out the issue.
In Swift the title of UIButton is white.
I changed the background color of parent View, now it is showing.
self.view.backgroundColor = UIColor.black

Resources