Changing backgroundcolor of a view seems hard? - objective-c

I want to change the background color of a view within the view controller. Selecting the view and opening the attributes inspector isn't showing that much. Should have an option here for the background.
Tried coding it within the (void)viewDidLoad{} with: self.view.backgroundColor = [NSColor blueColor]; That isn't working either, no object of NSView.
What am I doing wrong? I read that the coding problem probably has something to with the newer version of Xcode. coding issue

This is already questioned and answered in
Best way to change the background color for an NSView
Here the code for OS X
[self.view setWantsLayer:YES];
[self.view.layer setBackgroundColor:[[NSColor whiteColor] CGColor]];

wich xcode version are you using? This is how I change the backgroun color using the attributes inspector in Version 7.1 (7B91b)

Related

UISlider Disappeared in iOS 8.3

UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(50, 50, 100, 40)];
[self.view addSubview:slider];
The problem is that, I cannot view UISlider at all but I can still drag it. I have to guess the position and drag.
The compile environment is based on ios 6 sdk (mandatory because my app doesn't use flat yet)
Does anybody know how to fix this? Thank you very much.
I've also experienced this with an iOS 6.1 application running on iOS 8.3.
It appears that Apple have cleaned up the image resources which are for the old iOS appearance. I'm not sure if this was a mistake or intentional.
I found a solution to the issue using the methods provided to customise the appearance of the control which are detailed in the UISlider class reference.
Here's my code:
[self.myControl setThumbImage:[UIImage imageNamed:#"thumb.png"] forState:UIControlStateNormal];
[self.myControl setMaximumTrackImage:[UIImage imageNamed:#"background.png"] forState:UIControlStateNormal];
[self.myControl setMinimumTrackImage:[UIImage imageNamed:#"highlight.png"] forState:UIControlStateNormal];
I was able to re-create the standard appearance relatively easily.
I hope this helps.

iOS 7.1 Breaks UITabbar Images Selected State

Since updating to iOS 7.1, my tab bar images initialize as if they were all active, they are all highlighted on launch. Once I visit each tab, that tab image resets and displays correctly. Has anyone else seen this behavior? Suggestions for work around?
I rolled back my xcode to version 5.0, which the only old version i have and build the app using it. It works perfectly on iOS7.1 device now.
If you have the xcode version 5.0.2 that would be great also.
In my case, as I finally figured out, this issue was caused by setup of appearance protocol on UIView in my style controller which is called at startup.
I had been setting an overall tint color for UIView. It did not apply to the icons in iOS7.0 but that must have changed in 7.1.
My style code:
// Color for buttons and enabled controls
UIView *viewAppearance = [UIView appearance];
[viewAppearance setTintColor:overallTintColor];
I added this to fix:
[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor darkGrayColor]];
darkGrayColor is not ideal, I just threw it in there for testing. I tried using nil, as I would like it to just go back to the default as it used to. Giving nil as a color did not have any effect. I suppose I will play with some color values until I get a close match.

Background image of back button does not appear before it is touched iOS 7

I am experiencing some problems with [UIBarButtonItem appearance] for the the back button background image.
Normally (iOS 5 and iOS 6) I was able to set the background image of the back button like this:
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
But at iOS 7 the background image does not appear on the back button. The weird thing is, that the background image actually appears when the back button has been touched once.
I have tried setting the image for all states, to test if iOS 7 was using some kind of new state for an untouched back button, but that does not seem to be the case.
Do you have any idea, what I am doing wrong?
A solution which will make the background appear correctly on iOS7 is at OS 7 custom back button. It swizzles a method to fix the Apple bug (which is that they forget to call setNeedsDisplay on the private view when the background image is changed). Going borderless is probably better, if possible, but swizzling does work.
I searched for this problem and found that you are not the only one to have the same problem. There are many others who face the same issue with UIAppearance. These are the proofs (to explain you to your client) :
UIBackButton Background Image not appearing
Back button is not visible in iOS 7
In this case, what you can do is to follow the Answer provided in the 2nd Link.
You can either set the backIndicatorImage property on UINavigationBar to a custom image or you can change the color of the backIndicatorImage by setting the tintColor property on UINavigationBar.
You can create a custom UIBarButtonItem and manually assign it as UINavigationItem's leftBarButtonItem.
try to change the tint color of the button. There is some issue in iOS 7 for UIBarButton
I implemented a really nice solution that works under ios5+ here:
Back button item strangely disappearing under iOS7
To work with ios7 you need to use
UIImage *backButton = [[UIImage imageNamed:#"icon_back" resizableImageWithCapInsets:UIEdgeInsetsZero];
if ([UINavigationBar instancesRespondToSelector:#selector(setBackIndicatorImage:)]) {
[[UINavigationBar appearance] setBackIndicatorImage:backButton];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backButton];
}else{
//ios 5 and 6 code
}

How to dynamically size UITableView in Objective-C?

I'm just starting with iOS development and I was trying to achieve something that doesn't seem to work so far for me...
Here's the situation:
I have a nib file in which I have placed a UITableView, and just underneath a UIToolbar. That works just fine, the scaling is fine if I try different screensizes etc... So I was happy.
But here's the problem:
If the toolbar should be visible or not is a choice that a user can make somewhere in the application. However when the users selects to not see the toolbar I just call the method setHidden on the toolbar and pass it 'YES'.
The toolbar is now gone when I try this but the UITableView is not strechted to the bottom of the screen which gives me quite an ugly result.
So here's finally the question:
How can I automatically let the view stretch to the bottom when hiding toolbar? I guess I will have to do it in code (and not just some configuration option somewhere) but as I'm coming from Android this is somewhat strange for me.
Your best option will probably be to resize the tableview frame as you show/hide the tool bar.
CGRect frame = myTableView.frame;
frame.size.height += toolbar.frame.height;
myTableView.frame = frame;
Because you're using Auto-Layout, you'd want to create a height constraint for the UITableView and link it with your view controller via an IBOutlet. Then, to modify the height of the UITableView, simply do:
_tableViewHeightConstraint.constant += toolbar.frame.height;
You can even animate this with:
[UIView animateWithDuration:0.25 animations:^{
_tableViewHeightConstraint.constant += toolbar.frame.height;
}];
Note that you might need to call [_tableView layoutIfNeeded] after changing the height constraint.

Using a patternimage for a View inside an NSScrollView

I have a quite big problem, I am really not able to solve myself.
The result should look like this:
This image was made with photoshop and is part of the interface I try to build.
In the middle you see something, that should be a list of projects, you should be able to scroll, if it the list is bigger then the view.
So I am making a scrollview like this: (for some reason I cannot do this in the interface builder and want this to work programmatically)
NSScrollView *projectsListView = [[NSScrollView alloc] initWithFrame:NSMakeRect(15, 2, 801, 588)];
[projectsListView setHasVerticalScroller:YES];
Then I create the content view and set a pattern image as backgroundcolor:
NSClipView *contentView = [[NSClipView alloc] initWithFrame:NSMakeRect(0, 0,
[projectsListView frame].size.width, [projectsListView frame].size.height+(98*2))];
[contentView setBackgroundColor:[NSColor colorWithPatternImage:[NSImage imageNamed:#"BoxLineBackground"]]];
[contentView setDrawsBackground:YES];
Then set the view as document view:
[projectsListView setDocumentView:contentView];
Should work, right?
However the content view gets clipped and looks like this while scrolling:
I tried this to fix it, but it does nothing:
[[projectsListView documentView] setCopiesOnScroll:NO];
I also tried this, but it causes the contentview not to scroll at all.
The image stays the same, but I can move the scroller normally.
[[projectsListView contentView] setCopiesOnScroll:NO];
If I try to set the contentview with setContentView: instead of using setDocumentView:
it may work, but the scroller is gone, so it is also not working correctly.
I would really like to use the patternimage method, because I cannot tell how long the list will be. It depends on the user.
An additional problem then would be to get the whole thing rounded, but that does not matter that much. I tried to use a transparent border image and to overlay the NSScrollView with it using an NSImageView, but again this causes corruption, because it clips and moves the overlaying parts of the image view together with the content of the scrollview.
Anyone having an idea, how to achieve this?
Thanks
Rather than re-inventing the wheel, this interface should be implemented with a view-based NSTableView. The table cell UI could then be created in Interface Builder and you could control the background of the cells using the various NSTableView delegate methods. NSTableView will handle redraws upon scrolling correctly.
To handle the pattern color, just make the background of your cell a custom subclass of NSTableCellView and implement your pattern drawing code.
Regardless of all this, the problem you are having is due to an NSScrollView drawing optimisation. You can turn this off by calling [[yourScrollView contentView] setCopiesOnScroll:NO] on your NSScrollView instance. You can also set this in Interface Builder, just un-check the Copies on Scroll checkbox.
I fixed the problem by setting the Background Color on the NSScrollView instead on the NSClipView.
I though the background would be static in that case and I need to set it for the content view for that reason, but it works pretty well and does scroll together with the content view.
And thanks for Rob Keniger's answer. I will probably try this out.