UIView with UIButtons not showing up prior to click or rotate - ios

I've been banging my head with this issue for the last two days. Googled a lot but wasn't able to find the answer yet, so I decided to request some help here. Let's see if I get any luck.
I'm coding a reusable control that consists of an UIView with a variable number of customized UIButtons. I implemented initWithFrame:, initWithCoder: and drawRect: where the buttons (which are built prior to drawing) are actually added to the view. Everything is done programmatically since the UIButton content should be supplied when using the control, so there's no XIB for this UIView.
This UIView, let's call it CustomizableBarButton is then used in an UIViewController, let's call it MyTestViewController with a view on it, let's call it customizableBarButtonView.
MyTestViewController's GUI was set on IB where an UIView was tied to customizableBarButtonView (the class was matching accordingly).
MyTestViewController's is a pretty standard class except for the viewWillAppear: that initializes the buttons and passes them to the CustomizableBarButton along with some other options.
The issue is that everything works perfectly...except for the first time!
I mean, when I run the app on the simulator (I haven't tried it on the iPhone yet but I strongly believe that it's not an hardware issue) the MyTestViewController shows the customizableBarButtonView background but not the buttons. Now when you click on the place where a button should be all the buttons suddenly appear!
I'm puzzled since the CustomizablebarButton drawRect: runs before the strange "click n'appear" effect and the buttons are actually added to the subview.
Another hint that my help: if you don't click on the buttons (so you still got no buttons yet) but rotate the device they will also appear as if by magic!
It is probably something very simple but I'm missing it and I'm going nuts...
Can someone lend a hand on this, please?
Thanks in advance!

You said you're adding the buttons in drawRect:. Don't do that. You need to add the buttons in your init methods.

Related

Highlight UIButton when not selecting the button itself

I have a custom class here that consists of a UIButton inside of a UIView.
The goal of this class is to allow users to have a greater area (being the UIView) to select the button than just the buttons frame itself.
Now when a user taps on the view I want the buttons highlighted image to show... But the problem is, it does not.
I've read many possible solutions to this issue For Example:
Calling: [btnObject sendActionsForControlEvents:UIControlEventTouchUpInside]
This however did not change the buttons highlight.
I also tried just settings the button.highlighted = YES;
But that did not work either.
I have the images for different states properly setup (Normal and Highlighted), I'm sure of that.
I also have the gestureRecognizer working properly as the functionality is great except for the lack of highlight.
Does anybody know if I'm missing any thing special that needs to be done in order to pull off this seemingly very simple task? Surely it's been done many times.
Thank you
You were on the right track. -[UIButton setHighlighted:] is just a flag. What you need to do is call setNeedsDisplay on that button right after you change the highlighted property.
I solved my problem a little while ago and I'm not sure if Kevin Low's answer would've worked too, it very well might have.
But for some reason, a UITapGesture doesn't work well with highlighting buttons as a view transitions (That might be because I didn't call setNeedsDisplay). The gesture that ended up working was the UILongPressGesture with a 0.0 sec for minimum duration.

Storyboard connected properties not work

Im really confused because my problem may sound silly, i try to explain it clear - when i add anything on storyboard view controller it has no effect. And obvious, my viewController set properly to a controller, that manage storyboard scene. I adding anything - buttons, imageViews, it doesn't appear on a screen at all.
Also i want to add, i never seen such problem before recent Xcode update, when storyboard scene's become "large" and there is single scene for any device (600x600).
That issue literally drive me crazy, i can manage interface objects programmatically but i don't want to, because i use to work with Storyboard. Could you please write some advice or solution?
Thanks!
You need to add positioning constraints. By default, it will leave elements where they are, so they might be cut off. Also make sure what should be in the background is Sent to Back.
You can check if the auto layout this interfirindo or even if you this in the right view.

iOS: Add an image to the Navigation bar

I'm currently working on an iOS app that is pretty much supposed look and work like an already existing android app.
As a part of that, I'd like to make the top navigation look the same, but since iOs devices do not provide a hardware or software integrated back button, I'd also like to keep the back button navigation that's common to iOs apps.
I've tried numerous attempts to achieve it, but I'll stick with the latest I'm working on since it looks the most promising. I've started to work on a custom UINavigationBar class that overrides the initWithCoder: and layoutSubviews: methods. The result is as follows:
As you can see, the back button now overlaps the application icon. What I'm looking for is a way to make the button and the text to scale into the space right of the application icon. I've tried to handle this in layoutSubviews: but the superclass logic appears to be rather complex, I was not able to reproduce it's functionality (in particular it seems to be working with some private variables I don't have access to).
I also tried manipulating self.frame before calling [super layoutSubviews] but apart from endless loops I was not able to achieve anything in that direction.
I'd be happy if anyone could point me into the right direction.
If I understand your problem correctly, I think you could do this by adding a custom titleView to the UINavigationItem, and have an image view (on the left side for the icon) and a label (for the title) as subviews. This would replace the default title.

Single view iOS app with custom drawRect

I though I understood this, but I can't get it to work:
I'm trying to create a very simple app, to test various things (OK - it's an app to estimate Pi using the Monte Carlo technique by simulating throwing darts at a board).
I have a single view iOS app (e.g. from the single view template) on which I've simply got one UIButton (to launch the app) at the bottom and a UILabel at the top to show the results.
The view controller is a custom subclass of UIViewController call PiCalcViewController; the view is a standard UIView filling the whole screen.
The app works but now I want to be able to draw a graphic representation of how the simulation is going the middle of the screen, which I'll do in my view's drawRect (right?). So I thought that I'd create a new file (PiCalcView) an Objective-C class, make it a subclass of UIView and then, in IB, drag out a new view in the middle of my view controller and change it's class to PiCalcView.
Great, except that PiCalcView does not appear in the drop-down class list in the inspector.
Questions : Any idea what's wrong and is creating a subview of my UIView like this the right way to do it?
Type the class into the custom class section in the Identity Inspector.
OK - quitting and restarting Xcode appeared to do the trick.
Thanks for the tips - being new to Xcode & ObjC, I didn't know if I had the right technique.At least I know now.

add UIPanGestureRecognizer to a UIView

I have 2 UIViews; let's call'em mainView and otherView. mainView uses the whole screen (it is an iPad app) and otherView is a smaller one that appears on top of mainView. The problem is that I don't know how and where to add the UIPanGestureRecognizer. The code snippets I find shows the code, but its never clear (or I'm not sure why it never works).
Most of the time I get a "otherView may not respond to +addGestureRecognizer" warning, I've tried adding the UIGestureRecognixerDelegate in practically all headers file and I don't know what I'm doing wrong.
Hope you can help me out :)
addGestureRecognizer is not a class method, it's an instance method. Other than that, without code there's not much I can say.
Also, check out CS193P Lecture 8: Gesture Recognizers. iTunes Link

Resources