I have a UICollectionView with its cells all laid out.
I have this declared as a subview:
#property (nonatomic, strong) UIButton *aButton;
I then have that declared in each cell like so:
if (_aButton == nil)
{
_aButton = [UIButton buttonWithType:UIButtonTypeSystem];
}
// Add in all _aButton info here
[self.contentView addSubview:_aButton];
// Call to button pressed for button
[_aButton addTarget:self action:#selector(aButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
The button click method is like so:
- (IBAction) aButtonPressed:(UIButton *) sender
{
// Code never gets heree
}
The if(_aButton== `nil) is needed since cells get reused.
How do I make this work now? Thanks.
Add button action code before button added to view.... May be it will work .
// Call to button pressed for button
[_aButton addTarget:self action:#selector(aButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
// Add in all _aButton info here
[self.contentView addSubview:_aButton];
I think, the way you are initializing the button is going to give you frame as 0,0,0,0 . So first give it a proper frame and title to be visible on the screen.
Then try tapping on that title and see if it works or not.
It's unclear what you trying to achieve here, but the button is not working because you not setting it's position and size on the cell. It could be done by setting frame or layout constraints.
Try to set button frame:
_aButton.frame = CGRectMake(0.0f, 0.0f, 50.0f, 50.0f);
You can also set background color for the button, just to make it visible on the cell:
_aButton.backgroundColor = [UIColor redColor];
Everything else is correct, button should work.
So I'm trying to set the background color of a subclass (I think it's called) and it won't work :(
I have tried #interface BLAH BLAG BLAG : UIView
But that won't work
I'm not trying to change the WHOLE UIView class just the one in the photo
Can someone please explain this?
photo ~> http://m.imgur.com/UgtSP9U
#interface IGFeedItemActionCell : UIView
#end
%hook IGFeedItemActionCell
- (void)setBackgroundColor:(UIColor *)color
{
color = kDarkColor;
return %orig;
}
%end
this doesnt change anything ;( I would be grateful if someone could fix this for me :) thank you
UICollectionViewCells and UITableViewCells are slightly more complex to alter, as they're comprised of several other views. They also insert a selectedBackground view into selected cells in front of their normal backgroundView.
You can try changing the following cell properties:
self.backgroundView.backgroundColor = [UIColor redColor];
self.selectedBackgroundView.backgroundColor = [UIColor blueColor];
I have code that counts upwards when buttons on my app are pressed, but I'd also like to change the button's background color once it has been tapped.
Can I simply add a backgroundColor to the below code?
- (IBAction)buttonPressed {
count++;
totalLabel.text = [NSString stringWithFormat:#"Hours\n%li",(long)count];
}
#end
Sure. The easiest thing to do is change your IBAction so it receives the button that's been tapped.
-(IBAction)buttonPressed:(UIButton*)button {
button.backgroundColor = [UIColor redColor];
}
(You'll need to reconnect the outlet from the nib as well so it knows that the new selector is buttonPressed:, not just buttonPressed.
I'm trying to change UIViewController's backgroundColor.
I wrote the code in the viewDidLoad method. My other objects are shown on the screen without any problem but backgroundColor property did not work. The screen is gray. I also tried [UIColor redColor] or to write only this row in the viewDidAppear method but nothing changed.
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:0.15
green:0.15
blue:0.3
alpha:1.0];
//Add start button
[self.view addSubview:[self createButton:#"ButtonSample.png"
setTitleColor:[UIColor whiteColor]
buttonPositionX:(CGRectGetMidX(self.view.frame)-30)
buttonPositionY:(CGRectGetMidY(self.view.frame)+30.0)
buttonWidth:80.0
buttonHeight:20.0]];
//Add game title label
[self.view addSubview:[self createLabel:#"Vecihi"
labelFontName:#"Chalkduster"
labelFontSize:20
labelPositionX:CGRectGetMidX(self.view.frame)-25
labelPositionY:CGRectGetMidY(self.view.frame)
labelWidth:80
labelHeight:20
fontColor:[UIColor whiteColor]]];
//Add game logo imageView
[self.view addSubview:[self createImage:#"Spaceship"
imagePositionX:CGRectGetMidX(self.view.frame)-30
imagePositionY:CGRectGetMidY(self.view.frame)-80
imageWidth:40
imageHeight:60]];
}
I just tested it an it works just fine to set the color of a view controller's content view in viewDidLoad.
In my case, I had a view inside the content view that took up most of the visible area, so very little of my custom color was visible. I had to set the background color of my subview to clear before I could see the changed background color.
Is it possible that you have a subview that completely fills your content view and hiding it's background color?
If that's not the case, try adding a breakpoint to your viewDidLoad method to make sure it's being called.
Don't know why, my search bar in iOS 7 is leaving a right space. It's ok in iOS 6.
I know it has something to do with the section index, because if I remove it the space disappears, but I don't know how to fix it. Any thoughts?
Embed your UISearchBar in a UIView and then add that as the tableHeaderView. Structuring it that way in a storyboard worked for me. I'm guessing iOS resizes the UISearchBar in the tableHeaderView, but leaves a basic UIView alone (and doesn't bother to look inside it).
You might also want to make the section index transparent, which I did with:
[[UITableView appearance] setSectionIndexBackgroundColor:[UIColor clearColor]];
[[UITableView appearance] setSectionIndexTrackingBackgroundColor:[UIColor clearColor]];
Until a better answer appears, I just manually changed the frame of the search bar like this:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGRect barFrame = self.searchBar.frame;
barFrame.size.width = self.view.bounds.size.width;
self.searchBar.frame = barFrame;
}
I had this same issue with the iPhone 6/ 6Plus when using a SearchDisplayController. (Using Swift)
I tried setting the frame of the search bar but with no luck but i noticed that if i tapped on the textField of the UISearchBar and then cancelled it then it would take on the proper size of the view. I therefore managed to fix the issue by calling the code below in ViewDidLoad of the viewController using the search.
self.searchController.setActive(true, animated: false)
self.searchController.setActive(false, animated: false)
self.contactsTableView.sectionIndexBackgroundColor = [UIColor clearColor];
The reason for that white edge is because your index layer has a white background and is on top of the search bar. This should be sufficient.
Add the search bar inside a UIView put as tableView's header view. Set the tableview's sectionIndexBackgroundColor to clear color because it covers the header.
Tested with iOS 7, 7.1;
Because the table view always leaves 15px on the right for section Indexes View, so you should resize the Seach bar after reloading the table view
First:
self.tblData.sectionIndexBackgroundColor = [UIColor clearColor]; //(iOS >= 7 only)
Cheating time:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self performSelector:#selector(resizeSearchBar) withObject:nil afterDelay:0.01];
}
- (void) resizeSearchBar
{
CGRect frame = self.searchBar.frame;
if (frame.size.width < self.tblData.frame.size.width) {
frame.size.width = self.tblData.frame.size.width;
}
self.searchBar.frame = frame;
}
- (void) reloadTableData // call it anytime you want to reload table view
{
[self.tblData reloadData];
[self performSelector:#selector(resizeSearchBar) withObject:nil afterDelay:0.01];
}
Suggest
Dont cheat like me, just do the simpler way:
self.searchBar.searchBarStyle = UISearchBarStyleMinimal; // iOS >= 7 only
I also attached a UISearcBar in my application, and nothing is wrong there even my application supports rotation also.
Could you try removing and re creating UISearchBar in storyboard/xib
I added the search bar as a subview of the top-level view instead of the table view. Used autolayout to pin the searchbar to the top guide, and a vertical space constraint of 0 between the search bar and the table view.
The accepted solution with the method viewDidLayoutSubviews makes the screen flicker.
Instead what I did was create a subclass of UISearchBar that simply does this:
FullWidthSearchBar.h:
#interface FullWidthSearchBar : UISearchBar
#end
FullWidthSearchBar.m:
#import "FullWidthSearchBar.h"
#implementation FullWidthSearchBar
- (void)setFrame:(CGRect)frame {
frame.size.width = self.superview.bounds.size.width;
[super setFrame:frame];
}
#end
And then I assigned that class to the search bar on my xib:
The problem is the right white block, so if we change the block color the same as the search bar background, it looks normal.
just
if (IOS7) {
self.tableview.backgroundColor = [UIColor colorWithPatternImage:self.searchBar.backgroundImage];
}