0

I had created a floating button so that it can be always shown to the user on the top, but when ever I present a controller using the UINavigationController initiated, it hide behinds the presented view

I have a solution but it has to be implemented on every presented view controller.

I created a function by which I'm moving it to front, but for this I need to add code on every presented view controller. Is there any solution to do it a better way?

@objcMembers
final class MenuFloatingButton: UIView
{
static let tagValue = 99988


init(exitGuideTourButtonY: CGFloat)
{
    let calculatedFrame = CGRect(x: ExitTourButtonConstant.leftPadding, y: exitGuideTourButtonY, width: ExitTourButtonConstant.buttonWidth, height: ExitTourButtonConstant.buttonHeight)

    super.init(frame: calculatedFrame)

    frame = calculatedFrame
    layer.cornerRadius = ExitTourButtonConstant.buttonHeight * ExitTourButtonConstant.halfValue
    clipsToBounds = true
    tag = ExitGuideTourFloatingButton.tagValue

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapDetected))
    addGestureRecognizer(tapGesture)

    backgroundColor = ExitTourButtonConstant.buttonColor

    let titleLabel = UILabel(frame: bounds)

    titleLabel.text = ExitTourButtonConstant.buttonTitle
    titleLabel.textAlignment = .center
    titleLabel.font = ExitTourButtonConstant.buttonTitleFont
    titleLabel.backgroundColor = .clear
    titleLabel.textColor = .white

    addSubview(titleLabel)
}

required init?(coder aDecoder: NSCoder)
{
    super.init(coder: aDecoder)
    theUnexpectedHasHappened()
}
}

and I'm calling it like this

- (void)addExitGuideTourDemoButtonView:(CGFloat)exitGuideTourButtonY

{
UIView*   exitDemoButtonView = [self getExitButtonView];

if (exitDemoButtonView == nil)
{
    exitDemoButtonView = [[ExitGuideTourFloatingButton alloc] initWithExitGuideTourButtonY:exitGuideTourButtonY];

    [self.window addSubview:exitDemoButtonView];
}
else
{
    // Do nothing
}
}


- (UIView*)getExitButtonView
{
    UIView*   exitGuideTourDemoButtonView = [self.window viewWithTag:ExitGuideTourFloatingButton.tagValue];

    return exitGuideTourDemoButtonView;
    }
aheze
  • 24,434
  • 8
  • 68
  • 125
Parv Bhasker
  • 1,773
  • 1
  • 20
  • 39

2 Answers2

0

while you adding a button as a subview add alike

view.insertSubview(yourbutton,at index: 0)

Please refer to the below link. https://developer.apple.com/documentation/uikit/uiview/1622538-insertsubview

Or else you can add a button on the window.

var window :UIWindow = UIApplication.sharedApplication().keyWindow window.addSubview(yourButton)

Sahil Omer
  • 163
  • 9
  • I'm currenlty adding view in window, but its not working – Parv Bhasker Jun 01 '21 at 06:53
  • Try this guard let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else { fatalError("could not get scene delegate ") } sceneDelegate.window?.addsubview – Sahil Omer Jun 01 '21 at 10:04
0

Use bringSubviewToFront for yourButton as soon as you present the controller.

Sumit_VE
  • 429
  • 2
  • 12