I would like to put UIImageView that is shown on button click. That works good if I put it on the last place of Storyboard. The problem occurs when I do programatically some animation, and after that I click button for showing UIImageView and it is not on front position of the screen.
You can use bringSubviewToFront: method. Something like this:
[self.view bringSubviewToFront:imageView];
If you are adding and removing subviews, it can happen that the order of your views changes.
One solution is to leave the views where they are and only modify the hidden property or the alpha.
Another solution is to use insertSubviewAtIndex: instead of addSubview:.
Additionally, you can also use bringSubviewToFront: just to make sure.
Related
I want to show a logo UIView always on top when the app running,
I know there is a way to do that,add same UIView to every UIViewController,
but I think this is not the best way to do that.
when i have lot of pages,and modify the logo UIView,must modify it every page.
Did someone have better way to do this?
thanks.
look like this:
Since you only every have one window per app, and view's don't have levels, you have to make sure that view stays on top of the hierarchy, no matter what. One relatively easy way is to add it directly to the window above the rest of the interface (the navigation controller):
In applicationDidLaunch:
// After the main navigation controller or tab controller has been added
// either programmatically or in the xib:
UIImage *logo = [UIImage imageNamed:#"logo.png"];
UIImageView *logoView = [[UIImageView alloc] initWithImage:logo];
[self.window addSubview:logoView];
Actually, I think that (a) creating a subclass of UIView that shows your logo and has all the necessary setup in it and then (b) adding this subclass to each view controller is the cleanest and most manageable way to do this.
The reason I prefer this method over adding the view to the window is because if you ever have a view that you don't want to show the logo, you won't need to show and hide something you added to the window. Also, adding directly to the window may cause rotation challenges on certain iOS devices in my experience, depending on what you're doing.
Also, to make sure your logo view is always on top of the view hierarchy, you can do two things:
If the view already exists, you can bring it to front using [UIView bringSubviewToFront:]
[myParentView bringSubviewToFront:myLogoSubview];
If you are creating the view, it will be on top when you add it with [UIView addSubview:]
// Set up myLogoSubview first here with alloc+init, etc.
[myParentView addSubview:myLogoSubview];`
It looks like in your image you would replace myParentView with self.view and myLogoSubview with the view you're looking to keep on top, but this is just my assumption based on your image.
I'm struggling with this problem, and although there are quite some threads on this issue I've not figured it out yet.
I want to load a button into a subview of a scrollview. If I test this subview alone, the button works fine. Also, when I add the button to the scrollview directly, it works fine. But the two combined I don't get any touch event on the button.
My view hierarchy:
UIScrollView
UIView
UIButton (A)
UIButton (B)
So button B works fine, A doesn't.
I've tried messing around with the attributes like Cancellable Content Touches, but so far no luck. I'm loading the subview from the storyboard with:
ViewVC *viewVC = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewVC"];
From the posts I've read, this should just work, so either I'm missing something vital, or I've messed up some attributes along the way. Should I use a delegate to pass the events, or should this be done automatically?
Give your views some colours, and check if one of them is obscuring the other preventing it from being touched. Maybe you UIView is overlapping your UIButton(B), preventing it from being touched.
How are you sure you're adding it?
Make sure you're calling addSubview: on your UIScrollView with your subview as the parameter?
Here is a question for you:
I've got a UIButton that we'll call bottomButton. I have another UIButton that we'll call topButton. What I want to do is place topButton on top of bottomButton such that when you tap topButton, it's action is executed. The problem that I'm having is that when I tap topButton, its bottomButton's action that is getting executed.
So what I want to know is if it is possible to place a UIButton on another UIButton so that when tapped, only the top UIButton registers the tap. Savvy?
Any input is appreciated!
Yes, it's possible. There are three things to check out:
1) Is topButton actually situated above bottomButton in the view stack? [UIView bringSubviewToFront:] may help here.
2) Is topButton large enough to register the tap? Try making topButton larger than 44x44 and see if that helps.
3) Make sure that topButton is enabled.
EDIT
I just saw your comment about adding topButton as a subview of bottomButton, and that's likely to be part of the problem. You could try adding both bottomButton and topButton to a new UIView, and then add that new UIView where you had previously used bottomButton.
If top button is a subview of bottom it won't get the touch events passed to it because the bottom button's hitTest:withEvent: is called first and it will return YES.
You could try overriding hitTest:withEvent: for the bottom buttom to have it call hitTest on its subviews (normally it doesn't have UIControls in its subviews) and return that subview instead if it has a hit.
Ah, I see now from reading your comments. Since you're adding a button TO another button, you'll need to make the superview intercept the touch event.
Read this post:
How to make a superview intercept button touch events?
I'm not sure about this one but have you tried adding them to the view in different orders? Does that make a difference? Test it out and see what happens!
Why would you add a button to a button?
Try adding them to the same parent but putting topButton over bottomButton.
I am presenting an instance of UINavigationController modally with the modalPresentationStyle set to UIModalPresentationFormSheet. Inside the UINavigationController is an UITableViewController. For a delete button in the table view, I have to know the width of the sheet. Is there any way to access this?
I tried [self.view bounds], however this returns the with of the screen. I think that's because the view is not visible at the time a call this, however I don't know where else I should call it.
You can directly measure and store a magic value for the form sheet width (not pretty, I know, but it works) the value that I have been using is 540.
If you really want to get it programatically. You'll have to make sure that the view has been resized and prepped for presentation before taking the bounds value from the view. I believe this happens somewhere between viewWillAppear and viewDidAppear, although I can't remember exactly.
If all you want to do is ensure a button is aligned to the right, try using the autoresizingMask property of the button. I believe you'll only need to use UIViewAutoresizingFlexibleLeftMargin.
You can do "last minute" view setup and adjustments in viewDidLoad -- all the nib outlets have been initialized by that time.
I want to show a total inside a toolbar. I put in IB the UILabel on top of the toolbar .
However, when I run the app, the UILabel is totally invisible (but can set values on code fine).
The most bizarre thing is that in other form, all work fine. I don't see why in one form work but not in another...
Any idea in how fix this? Or why is this behaviour happening?
Don't use a UILabel.
Use a UIBarButtonItem. Then set it to style: plain. It looks like a label, but it actually borderless button. This is the general practice of displaying text on a bar.
You can also create UIBarButtonItem with a custom view in code. You are simple "wrapping" the UILabel in a UIBarButtonItem allowing you to add anything you want to a tool bar.
To add in response to comment:
Either way, you make the button "inactive" and it doesn't respond to touches. Even though it is a button, it doesn't appear to be one. This is how Apple expects to add views to a toolbar/navbar as apposed to "float things on top of it". It violates no HIG guidelines, much the opposite, it is a reccomended design technique.
To stop the glow:
Create the button programmatically, make sure it is disabled, add it to the bar, it should then be disabled, but not dim.
In IB, have you tried to select the label and use the "Bring to Font" menu item (under Layout)? It seems like you are trying to do something pretty standard.
When you try to set values, is the label coming up as nil or at address 0x0? It's possible that the label is there, but its text cannot be set because its instance is faulty (not properly connected in IB to the IBOutlet).... Just put a breakpoint on the line where you are trying to set the value(s) for the label, and verify that the label variable is not nil (or 0x0). If it's not, try setting the text and verify on the next line that its text was set properly.
drag a UIButton into your UIToolBar. Then uncheck User Interaction Enables for this button.
Customize your UIButton so that it will look like a UILabel. Hope this will help you.