UITapGesture Works only a part - iphone

Recently i tried to create a custom segmentedControl where i had three views in it.
WHILE I WAS TOUCHING FIRST HALF OF THE VIEW THE tapGesture WORKED. But the rest half dint respond.
My frame of the segment is:
segment.frame=CGRectMake(0, 0, 300, 100) ;
And the frame of the UIView is:
view1 = [[UIView alloc]initWithFrame:CGRectMake(10, 80, 100, 40)];

change the height of the frame to 200 And then it will work:
segment.frame=CGRectMake(0, 0, 300, 200) ;
Reason: Your height of the view is less when compared.

Put Your view frame like this it will work for you..Because you add Segment with height 100 from y=0, So you can set view frame after y = 100..
view1 = [[UIView alloc]initWithFrame:CGRectMake(10, 110, 100, 40)];

Related

Controlling size / position of UINavigationItem titleView

In my app, I have some custom titles (with lettering that isn't a font) stored in pngs that I want to put as the title of my navigation. I want the lettering in the titles all to be the same size for each different view controller, so in illustrator I've worked on making it all the same size and width (affording blank space to account for shorter strings). I do the following:
UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SelectAnAlbumTitleLettering"]];
titleImageView.contentMode = UIViewContentModeCenter;
self.navigationItem.titleView = titleImageView;
[titleImageView release];
And it seems the image is arbitrarily resized and positioned based on the elements included on each navigationBar (i.e. back button, right button etc.).
I'm wondering, how can I gain control of titleView so I can make it implement the size and position that I want and so it isn't arbitrarily resized / repositioned.
Instead of using your image view as the title view, create another view that contains the image view. Set that view as the title view of the navigation item; now you’re free to adjust the image view’s frame within the title view.
You can control size and position of UINavigationbar titleview. Don't set imageview as titleview directly. Instead create a custom UIView and then set its frame as per your requirement and add your titleImageView as its subview.
UIView *backView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];// Here you can set View width and height as per your requirement for displaying titleImageView position in navigationbar
[backView setBackgroundColor:[UIColor greenColor]];
UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SelectAnAlbumTitleLettering"]];
titleImageView.frame = CGRectMake(45, 5,titleImageView.frame.size.width , titleImageView.frame.size.height); // Here I am passing origin as (45,5) but can pass them as your requirement.
[backView addSubview:titleImageView];
//titleImageView.contentMode = UIViewContentModeCenter;
self.navigationItem.titleView = backView;
Hope it helps you.
In Swift, you can do it like this:
var titleView = UIView(frame: CGRectMake(0, 0, 100, 40))
var titleImageView = UIImageView(image: UIImage(named: "cocolife"))
titleImageView.frame = CGRectMake(0, 0, titleView.frame.width, titleView.frame.height)
titleView.addSubview(titleImageView)
navigationItem.titleView = titleView
Updated Swift4 code:
var titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 40))
var titleImageView = UIImageView(image: UIImage(named: "headerlogo"))
titleImageView.frame = CGRect(x: 0, y: 0, width: titleView.frame.width, height: titleView.frame.height)
titleView.addSubview(titleImageView)
navigationItem.titleView = titleView
Update swift 5
var titleView = UIView()
navigationItem.titleView?.translatesAutoresizingMaskIntoConstraints = false
navigationItem.titleView = titleView

How to fix UILabel alignment in UINavigationBar

I put a custom UILabel in my UINavigation bar like this:
UILabel *navTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
navTitle.backgroundColor = [UIColor clearColor];
navTitle.text = #"TEST";
navTitle.font = [UIFont fontWithName:#"GothamNarrowBook-Regular" size:28];
navTitle.textColor = [UIColor whiteColor];
navTitle.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navTitle;
But when it shows up on the emulator, its aligned too high:
I have had no luck with adjusting the frame. Any ideas?
You can put label inside another view, and insert this another one as titleView. That will allow some kind of flexibility.
You can also try to adjust position of the frame:
UILabel *navTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, >>>10<<<, 200, 44)];
But remember, that NavigationBar has different height in landscape mode, so you have to use autoresizingMask accordingly.
Can you set the frame of self.nagivationItem.titleView and push it down?
CGRect initialFrame = self.navigationItem.titleView;
self.navigationItem.titleView = CGRectMake(initialFrame.origin.x, initialFrame.origin.y+10, initialFrame.size.width, initialFrame.size.height);
Another thing to check, make sure your UINavigationBar is actually 44px tall, if your UILabel is the same height then I believe default behavior is to center vertically which would land it in the correct position.
I take it that increasing the height of the UILAbel doesn't work.
Or the Y position.
And I take it you tried adjusting the frame after it's created, e.g. other than in the initWithFrame.
Maybe try using an image instead? Well it's an idea ;)

Custom tab bar setting the frame and position

I'm currently in the process of making my custom tab bar. It's working fine, the way I did it was by hiding the default bar. I have my images prepared, the on and off ones.
There seems to be an implementing of these into the main concept by using:
btn3.frame = CGRectMake(240, 430, 80, 50);
The number combination is unique for each CGRectMake. I have done it, but i'm using 3 buttons on the bar. This tutorial uses four. What should I set the number combination for each CGRectMake if i'm using 3 buttons and not 4. Because I assume that the combos are proportional to each other depending on how many tabs there are. All it is, is setting the frame size and position of the button/tab.
I used these for my tabs, but I got a gap in between.
btn1.frame = CGRectMake(0, 430, 80, 50);
btn2.frame = CGRectMake(160, 430, 80, 50);
btn3.frame = CGRectMake(240, 430, 80, 50);
http://www.rumexit.co.uk/2010/11/how-to-customise-the-tab-bar-uitabbar-in-an-iphone-application-part-2-of-2/
it's in the fourth box on the page.
// Pseudocode.
int numButtons = 3;
float buttonWidth = self.view.frame.size.width / numButtons;
btn1.frame = CGRectMake(0 * buttonWidth, 430, buttonWidth, 50); btn2.frame = CGRectMake(1 * buttonWidth, 430, buttonWidth, 50); btn3.frame = CGRectMake(2 * buttonWidth, 430, buttonWidth, 50);

UIProgressView defaulting to top left corner of main view

My UIProgressView is going to the top right hand corner of the main view - even though I set it as a subview of a UIView (which in turn is a subview of the main view). And even if I setFrame:.
Any ideas?
if(downloadBar == nil){
downloadBar = [[UIProgressView alloc] initWithFrame:CGRectMake(10, 10, 200, 10)];
}
[downloadBar setProgressViewStyle:UIProgressViewStyleDefault];
[downloadBar setProgress:([download downloadStatus]/100.0f)];
[downloadView addSubview:downloadBar];
// download view initialisation
// this is initalised BEFORE the download bar
downloadView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];
[downloadView setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:downloadView];
[downloadView retain];
Thanks
I think it depends on your downloadView. Because the origin coordinate (0, 0) of downloadBar will be at the same position of the origin coordinate (0, 0) of the downloadView. So, if you downloadView is located in top right, the downloadBar will be the same

How to add label above UIProgressView in UIToolbar?

I have a UIToolbar in which I have placed a UIProgressView successfully. However, I have seen some apps contain a small label above the UIProgressView which tells the user what the program is doing where progress is being made -- e.g. to download a file. However it seems that this cannot be done in UI Builder. Any ideas on the best way to add the label ablve the UIProgressView in the toolbar? Here is what I am interested in:
+------------------------------------------------+
| Uploading File |
| ================-------------------- [CANCEL] |
+------------------------------------------------+
Make a custom UIView that contains a UILabel and UIProgressView as subviews. You then insert the custom UIView into the toolbar.
You can actually also add text directly to a UIProgressView as a subview, ex:
UIProgressView *videoProgressView = [[UIProgressView alloc] initWithFrame:CGRectMake(40, self.view.frame.size.height/2, self.view.frame.size.width - 80, 40)];
UILabel *processing = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, videoProgressView.frame.size.width, 25)];
processing.text = #"Processing Video...";
processing.textAlignment = NSTextAlignmentCenter;
[videoProgressView addSubview:processing];
[self.view addSubview:videoProgressView];
Just make sure UIProgressView's clipsToBounds property is set to NO.
Here's a Swift 5 implementation of Lyndsey Scott's answer used in my project:
let view = UIApplication.topViewController()!.view!
let progressView = UIProgressView(progressViewStyle: .default)
progressView.center = view.center
progressView.trackTintColor = .gray
progressView.frame = CGRect(x: 40, y: view.frame.size.height / 2, width: view.frame.size.width - 80, height: 40)
let progressViewLabel = UILabel(frame: CGRect(x: 0, y: -50, width: progressView.frame.size.width, height: 25))
progressViewLabel.text = "Migrating database:"
progressViewLabel.textAlignment = .center
progressView.addSubview(progressViewLabel)
view.addSubview(progressView)