iOS8 UIButton title disappear - objective-c

I have a button in my app that is perfectly visible on iOS7. But when I switch to iOS8 the title label for this button does not appear. And I can see only border around the button.
Here is the code that makes the button. Could someone give me a clue where the problem is?
UIButton *problem = [[UIButton alloc] initWithFrame:frame];
[problem addTarget:self action:#selector(problemButton:) forControlEvents:UIControlEventTouchUpInside];
problem.backgroundColor = [UIColor whiteColor];
[problem setTitle:NSLocalizedString(#"ReportAProblem", nil) forState:UIControlStateNormal];
problem.titleLabel.textColor = CUSTOM_DARK_GRAY_COLOR;
[[problem layer] setBorderWidth:1.0f];
[[problem layer] setBorderColor:CUSTOM_DARK_GRAY_COLOR.CGColor];
problem.layer.cornerRadius = 5;
problem.clipsToBounds = YES;
I found an answer for this question but immediately there is another:
I encounter the very same problem with some of my UILabels, for example:
self.label = [[UILabel alloc] initWithFrame:labelFrame];
self.label.numberOfLines = 1;
self.label.adjustsFontSizeToFitWidth = YES;
self.label.textAlignment = NSTextAlignmentCenter;
self.label.textColor = self.textColor;
CGFloat fontSize = self.bounds.size.height / 5;
self.label.font = [UIFont systemFontOfSize:fontSize];

You should use - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state instead of titleLabel.textColor.
Second problem: Why are you trying to set a ratio of your view height as a font size ? Maybe doing it with your label makes more sense. CGFloat fontSize = self.label.bounds.size.height / 5; Anyway what you usually want to do is have a fixed font size and adapt the label frame around it.

Related

UIViews Overlap after adding Gravity and Collision behaviour

I am trying to stack up uiviews on click of a button. I've also added Gravity & Collision behaviour using UIKit dynamics. The problem as you can see from the image (http://pho.to/7nVJI) is, the lower most green block and the block on top of that seems to be overlapping. Here is the ViewDidLoad method :
`- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *moveIt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
moveIt.frame = CGRectMake(200, 50, 70, 30);
[moveIt addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:moveIt];
moveIt.titleLabel.text = #"Hit it";
moveIt.titleLabel.textColor = [UIColor whiteColor];
moveIt.backgroundColor = [UIColor blueColor];
UIButton *addBlockButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addBlockButton.frame = CGRectMake(20, 50, 70, 30);
[addBlockButton addTarget:self action:#selector(addBlock:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:addBlockButton];
addBlockButton.backgroundColor = [UIColor orangeColor];
animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
newBlockYCoordiante = self.view.bounds.size.height-BLOCKHEIGHT;
UIView *basicBlock = [[UIView alloc] initWithFrame:CGRectMake(64, newBlockYCoordiante, BLOCKWIDTH, BLOCKHEIGHT)];
basicBlock.layer.borderWidth = 1;
basicBlock.tag = 100;
basicBlock.layer.borderColor = [UIColor greenColor].CGColor;
basicBlock.backgroundColor = [UIColor clearColor];
[self.view addSubview:basicBlock];
newBlockYCoordiante = newBlockYCoordiante - BLOCKHEIGHT;
if (collisonBehaviour == nil) {
collisonBehaviour = [[UICollisionBehavior alloc] initWithItems:#[basicBlock]];
collisonBehaviour.translatesReferenceBoundsIntoBoundary = YES;
collisonBehaviour.collisionDelegate = self;
[animator addBehavior:collisonBehaviour];
}
if (gBehaviour == nil) {
gBehaviour = [[UIGravityBehavior alloc] initWithItems:#[basicBlock]];
[animator addBehavior:gBehaviour];
}
}`
And here is how I add new blocks on top of the previous blocks :
- (void) addBlock :(id)sender
{
UIView *basicBlock = [[UIView alloc] initWithFrame:CGRectMake(64, newBlockYCoordiante, BLOCKWIDTH, BLOCKHEIGHT)];
basicBlock.layer.borderWidth = 1;
basicBlock.layer.borderColor = [UIColor orangeColor].CGColor;
basicBlock.backgroundColor = [UIColor clearColor];
[self.view addSubview:basicBlock];
newBlockYCoordiante = newBlockYCoordiante - BLOCKHEIGHT;
[gBehaviour addItem:basicBlock];
[collisonBehaviour addItem:basicBlock];
}
How do i ensure that these blocks doesn't overlap while stacking up. This overlapping causes another issue, when I add another tower similar to this next to it on right and add push behaviour to the green block to move it towards right, instead of moving only the adjacent block from the second tower it also moves the second last block from the 2nd tower.
Any pointers/help is appreciated. Thanks in advance :).
I believe UIDynamics adds a little "give" to objects and friction so that when they collide and move they act more realistically. You may be able to reduce this, by using UIDynamicItemBehavior and reducing elasticity and bounce, but I'm not sure.
Long story short UIDynamics tends to be an imprecise system and if you're looking to animate interface items in, I'd recommend using dynamics only until items reach their desired location and then removing their behaviors and setting their exact locations :)

iOS 7 BarButtonItem with image and title

I'm trying to figure out how can I achieve something like this:
This is a toolbar and I'd like to keep the button title text without having to create the whole image with icon AND text. How can I add the icon to the left of the UIBarButtonItem while keeping the text set with:
UIBarButtonItem *customBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Button" style:UIBarButtonItemStyleBordered target:nil action:nil];
EDIT
This is what I achieved with the following code:
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setImage:[UIImage imageNamed:#"Test"] forState:UIControlStateNormal];
[customButton setTitle:#"Button" forState:UIControlStateNormal];
customButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
[customButton sizeToFit];
UIBarButtonItem *customBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
Two issues here: the first is the size of the UIButton and the second is that, as you can see from the gif, the button doesn't animate properly when I tap inside the button and when I release the tap. Any ideas?
You can embed a UIButton as a custom view inside your UIBarButtonItem. You can create your UIButton however you want, including with an image and text, using -setImage:forState: and -setTitle:forState:.
UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setImage:[UIImage imageNamed:#"image"] forState:UIControlStateNormal];
[customButton setTitle:#"Button" forState:UIControlStateNormal];
[customButton sizeToFit];
UIBarButtonItem* customBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.leftBarButtonItem = customBarButtonItem; // or self.navigationItem.rightBarButtonItem
Note that when doing this, you'll need to attach your IBAction methods to customButton, not to customBarButtonItem.
For more info, see the documentation for initWithCustomView:.
You can also do all of this in interface builder just by dragging a UIButton to the left bar button/right bar button slot on a navigation bar.
I was not satisfied with all solutions found on stack overflow becauseI like how UIBarButtonItenm convert images so they are displayed correctly and they can be changed with tint colour. So i discovered this solution that I can use nicely with not much code... End effect is similar to toolbar in facebook and other current apps...
When you change tint colour of view contained in UIBArButtonItem colours of image and title change accordingly, really cool , I do not have to create special images at all.
This code is called from each button like that Competition Button, and also Button is set as referencing outlet...
- (void)selectButton:(UIButton *)sender {
_competitionButton.superview.tintColor = [UIColor lightGrayColor];
_usersButton.superview.tintColor = [UIColor lightGrayColor];
_managementButton.superview.tintColor = [UIColor lightGrayColor];
sender.superview.tintColor = [UIColor whiteColor];
}
- (IBAction)onCompetitionButtonClick:(UIButton *)sender {
[self selectButton:sender];
[_scrollView scrollToPage:0 of:3];
}
In Button like that titled Competition there is only title and in bar button item there is image... This is how I have setup it and it works , maybe there are also other possibilities..
So I managed to do my solution programatically... Some users where complaining it does not work ... So how to create UTToolbar with image and item so that you can change color of button by tint and it magically alters image as well as title label ? My UI editor solution works for sure...
In this code can clarify things more and you can do it programatically either...
- (void)loadToolbar {
NSMutableArray *items = NSMutableArray.new;
[items add:[UIBarButtonItem createWithItem:UIBarButtonSystemItemFlexibleSpace :nil :nil]];
for (uint pageIndex = 0; pageIndex < _controllers.count; ++pageIndex) {
UIView *itemView = [UIView.alloc initWithFrame:CGRectMake(0, 0, 100, 44)];
itemView.backgroundColor = [UIColor clearColor];
itemView.tintColor = [UIColor orangeColor];
[itemView addSubview:[self createToolbarForItemView:pageIndex]];
[itemView addSubview:[self createButtonForItemView:pageIndex]];
UIBarButtonItem *item = [UIBarButtonItem.alloc initWithCustomView:itemView];
item.style = UIBarButtonItemStylePlain;
[items add:item];
[items add:[UIBarButtonItem createWithItem:UIBarButtonSystemItemFlexibleSpace :nil :nil]];
}
[_toolbar setItems:items];
}
- (UIButton *)createButtonForItemView:(uint)pageIndex {
UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeSystem];
itemButton.frame = CGRectMake(0, 0, 100, 44);
itemButton.titleLabel.font = [UIFont systemFontOfSize:11];
itemButton.contentEdgeInsets = UIEdgeInsetsMake(30, 0, 0, 0);
itemButton.text = [_controllers[pageIndex] tabTitle];
itemButton.touchUp = ^(UIButton *sender) {
for (UIButton *button in _buttons) button.superview.tintColor = UI.toolBarInactiveButtonTextColor;
sender.superview.tintColor = UI.toolBarActiveButtonTextColor;
[self showPage:pageIndex];
};
[_buttons add:itemButton];
return itemButton;
}
- (UIToolbar *)createToolbarForItemView:(uint)pageIndex {
UIToolbar *itemToolbar = [UIToolbar.alloc initWithFrame:CGRectMake(0, 0, 100, 44)];
itemToolbar.tintColor = nil;
itemToolbar.barStyle = _toolbar.barStyle;
itemToolbar.translucent = _toolbar.translucent;
UIBarButtonItem *imageItem = [_controllers[pageIndex] tabImageItem];
imageItem.style = UIBarButtonItemStylePlain;
imageItem.tintColor = nil;
imageItem.imageInsets = UIEdgeInsetsMake(-10, 0, 0, 0);
itemToolbar.items = #[
[UIBarButtonItem createWithItem:UIBarButtonSystemItemFlexibleSpace :nil :nil],
imageItem,
[UIBarButtonItem createWithItem:UIBarButtonSystemItemFlexibleSpace :nil :nil]
];
return itemToolbar;
}
I solved this problem as follows:
[Button **setTitleColor**:[UIColor colorWithRed:129/255.0f green:124/255.0f blue:110/255.0f alpha:1.0f] forState:**UIControlStateHighlighted**];

UINavigationBar - change UIBarButtonItem positions

I am using a UINavigationController and its bar within my app.
Now I want to change the leftBarButtonItem and rightBarButtonItem positions.
I want them to be on a different x and y position having a custom witdth and height.
But the problem is, that changing the frame does not change anything with the barButtonItem. I do not now why, but the frame always gets resetted to its original coordinates.
Here is my code which I call in viewWillAppear:
UINavigationItem *navItem = [[self.navigationBar items] lastObject];
UIView *rightview = [[UIView alloc] initWithFrame:CGRectMake(0,0,66,30)];
rightview.backgroundColor = [UIColor blueColor];
//add custom view
UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithCustomView:rightview];
navItem.leftBarButtonItem = search;
The view is not starting at 0/0, the x position for example is at 5px insetead of 0.
Any suggestions on how to solve this issue?
This worked for me for the leftBarButtonItem:
If you want the UIBarButtonItem to show exactly at position (0, 0), you can create a UIButton, sets its frame to (-5, 0, width, height), assuming that the offet of a left barbutton item is (5, 0), and place that in a UIView that you set as leftBarButtonItem.
#import "UINavigationItem+CSAButtons.h"
#implementation UINavigationItem (CSAButtons)
- (void) makeLeftBarButtonWithTarget:(id)target action:(SEL)action {
UIImage * imageNormal = [UIImage imageNamed:#"normal.png"];
UIImage * imageHighlighted = [UIImage imageNamed:#"highlighted.png"];
UIImage * imageDisabled = [UIImage imageNamed:#"disabled.png"];
// create your button
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.exclusiveTouch = YES;
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:imageNormal forState:UIControlStateNormal];
[button setBackgroundImage:imageHighlighted forState:UIControlStateHighlighted];
[button setBackgroundImage:imageDisabled forState:UIControlStateDisabled];
// set the frame of the button (better to grab actual offset of leftbarbuttonitem instead of magic numbers)
button.frame = CGRectMake(-5.0, 0.0, imageNormal.size.width, imageNormal.size.height);
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imageNormal.size.width, imageNormal.size.height)];
[view addSubview:button];
// set the barbuttonitem to be the view
UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
self.leftBarButtonItem = barButtonItem;
}
I dont think you have the ability to move a UIBarButtonItem but you might be able to achieve the same effect by adding that UIView element as a subview to the navigationBar (or navItem). You'll need to play with it a bit. It should allow for much more flexibility.
hope this helps.

How to disable vertical scrolling in UIScrollView (Obj-C)

I want to disable vertical scrolling from my UIScrollView if possible.. My code is like below.. Working fine except users can scroll up and down which shouldn't be there I believe.. Thanks in advance..
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height / 3)];
scroll.contentSize = CGSizeMake(scroll.contentSize.width,scroll.frame.size.height);
scroll.pagingEnabled = YES;
scroll.backgroundColor = [UIColor blackColor];
int xVal = 30;
NSInteger numberOfViews = 5;
for (int i = 0; i < numberOfViews; i++) {
UILabel *testLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 0, 90, 100)];
UILabel *testLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 20, 90, 100)];
UILabel *testLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 40, 90, 100)];
testLabel2.backgroundColor = [UIColor clearColor];
testLabel2.text =#"Test1";
testLabel2.textColor = [UIColor whiteColor];
testLabel2.font = [UIFont boldSystemFontOfSize:12];
testLabel1.backgroundColor = [UIColor clearColor];
testLabel1.text =#"Test2";
testLabel1.textColor = [UIColor whiteColor];
testLabel1.font = [UIFont boldSystemFontOfSize:12];
testLabel3.backgroundColor = [UIColor clearColor];
testLabel3.text =#"Test3";
testLabel3.textColor = [UIColor whiteColor];
testLabel3.font = [UIFont boldSystemFontOfSize:12];
xVal += 120;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(xVal, 30, 150, 130)];
view.backgroundColor = [UIColor blackColor];
xVal += 200;
[scroll addSubview:testLabel1];
[scroll addSubview:testLabel2];
[scroll addSubview:testLabel3];
[scroll addSubview:view];
}
[self.view addSubview:scroll];
In my situation, I was unable to get the height of the scrollview (due to autolayout, I wasn't able to get the height in viewDidLoad). You can add this to the delegate method.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
}
you must set your scrollview content height to the scroll view height
CGSize scrollableSize = CGSizeMake(scrollableWidth, yourScrollViewHeight);
[myScrollView setContentSize:scrollableSize];
Here may be a possible duplicate
disabling vertical scrolling in UIScrollView
or you can also try this:
self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * number_of_items, 1);
Assuming it's an iPhone app, so the screen resolution is 320×480 .
Now you are setting your scroll view's height as self.view.frame.size.height / 3 .
Here your view's height is actually taken as 460 and not 480 (20px for staus bar).
So when you add the other view as subview to your scroll view, its frame goes out of the scroll's content view. So you need to manage this while setting your frames/content size.
Let me know if this works for you.
There is no problem with that simply change the contentSize of your UIScrollView and you are done.Increase its width size and its height should be as it is at present.Moreover you can also hide the vertical scrollers also.
scroll.showsVerticalScrollIndicator = NO;
scroll.contentSize = CGSizeMake(scroll.contentSize.width + xVal,scroll.frame.size.height);
You should do like this:
aScrollView.scrollsToTop = NO;
aScrollView.delegate = self;
aScrollView.contentSize = CGSizeMake(aScrollView.frame.size.width * X, aScrollView.frame.size.height/2);
In your xml file there are two properties are available for scrollview are horizontal scroll and vertical scroll. as per your requirement you can check or uncheck and if you want to stop vertical or horizontal scroll then you have to make same content size of scrollview with height or width of scrollview respectively

UINavigationController rightbarbuttonitem not showing

after going through the questions related to UINavigationController item i came to post my exact problem ..
firstly i am not add a UINavigationController in MainWindow.xib i created and adding UINavigationController in AppDelegate file
after that in a UIViewController (not in rootViewController) class I have to add a rightbarbutton of Done type I am
adding it in a searchbarDelegate Method showing as bold:-
- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
//This method is called again when the user clicks back from teh detail view.
//So the overlay is displayed on the results, which is something we do not want to happen.
if(exhibDataObj.searching)
return;
//Add the overlay view.
if(ovController == nil)
ovController = [[OverlayViewController alloc] initWithNibName:#"OverlayView" bundle:[NSBundle mainBundle]];
CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
//Parameters x = origion on x-axis, y = origon on y-axis.
CGRect frame = CGRectMake(0, yaxis, width, height);
ovController.view.frame = frame;
ovController.view.backgroundColor = [UIColor grayColor];
ovController.view.alpha = 0.5;
ovController.rvController = self;
[self.theTableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];
exhibDataObj.searching = true;
letUserSelectRow = NO;
self.theTableView.scrollEnabled = NO;
//Add the done button.
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(doneSearching_Clicked:)];
}
but this never works .
Any help will be more appreciable.
I've tested your code real quick and it seems to be working fine for me, however, I've had the same problem once back then it turned out I was setting the rightBarButtonItem to nil in my viewDidLoad method after setting it to a button. you should make sure your not making the same mistake somehow.
However if that does not work for you you can always try setting a custom button to it:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 30, 30)];
button.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"done.png"]];
[button addTarget:self action:#selector(doneButtonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationController.visibleViewController.navigationItem.rightBarButtonItem = customItem;