An attempt to programmatically create a rounded rectangular button in an Objective-C View Controller in Xcode 6 and iOS 8.1 only produces a non-rounded rect button. The UIButton buttonWithType: method is being used.
Why?
//
// ViewController.m
//
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)loadView // Called the first time the view property is accessed.
{
CGRect viewRect = [[UIScreen mainScreen] bounds];
UIView *view = [[UIView alloc] initWithFrame:viewRect];
self.view = view;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the view's background color.
self.view.backgroundColor = [UIColor colorWithRed:0.462 green:0.999 blue:0.999 alpha:1.0];
// Create a button.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Place and size the button on the screen.
btn.frame = CGRectMake(100, 100, 90, 44);
// Set the button's background color.
btn.backgroundColor = [UIColor yellowColor];
// Set the button's normal title.
[btn setTitle:#"Test Button" forState:UIControlStateNormal];
// Add the button as a subview.
[self.view addSubview:btn];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Add this line and try:
btn.layer.cornerRadius = 5;
Related
All I am trying to do is add a new view to the content view of my NSWindow instance. When I do the following I do not see the new view (which should be black and take up the entire window). What am I doing wrong?
(done in response to a button click)
NSRect frameRect = [self.window frame];
frameRect.origin = NSZeroPoint;
NSView *view = [[NSView alloc] initWithFrame:frameRect];
view.wantsLayer = YES;
view.layer.backgroundColor = [NSColor blackColor].CGColor;
[self.window.contentView addSubview:view];
I've set up a simple project with a push button inside a ViewController and got a warning for accessing self.window. When using self.view.windowthe warning went away and your provided code works as expected.
Updated code
NSRect frameRect = [self.view.window frame];
frameRect.origin = NSZeroPoint;
NSView *view = [[NSView alloc] initWithFrame:frameRect];
view.wantsLayer = YES;
view.layer.backgroundColor = [NSColor blackColor].CGColor;
[self.view.window.contentView addSubview:view];
Update
Assuming that you're using an instance of WindowController where you're adding a button programmatically, your code works as expected.
#implementation WindowController
- (void)windowDidLoad
{
[super windowDidLoad];
CGRect buttonRect = CGRectMake(self.window.frame.size.width / 2 - 50,
self.window.frame.size.height / 2,
100,
50);
NSButton *button = [[NSButton alloc] initWithFrame:NSRectFromCGRect(buttonRect)];
[button setTitle: #"Click me!"];
[button setTarget:self];
[button setAction:#selector(buttonPressed)];
[self.window.contentView addSubview:button];
}
- (void)buttonPressed
{
NSRect frameRect = [self.window frame];
frameRect.origin = NSZeroPoint;
NSView *view = [[NSView alloc] initWithFrame:frameRect];
view.wantsLayer = YES;
view.layer.backgroundColor = [NSColor blackColor].CGColor;
[self.window.contentView addSubview:view];
}
An instance of NSViewController ain't got a property of window - only NSWindowController has one.
I am trying to draw animated gif on my screen in mac OSX app .
I used this code to insert the gif: I can see the Gif as 1 picture it doesn't animates
only static picture :( what should I add to make it animated ?
#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>//for drawing circle
#import "sharedPrefferences.h"
#interface GenericFanSubView : NSView
{
NSColor * _backgroundColor;
NSImageView* imageView;
}
- (void)setBackgroundColor :(NSColor*)color;
- (void)insertGif1;
- (void)insertGif2;
- (void)insertGif3;
#end
#import "GenericFanSubView.h"
#define PI 3.14285714285714
#implementation GenericFanSubView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
imageView = [[NSImageView alloc]initWithFrame:CGRectMake(0, 0,self.frame.size.width,self.frame.size.height)];
[imageView setAnimates: YES];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
// Drawing code here.
[self drawCircleInRect];
_backgroundColor = [NSColor whiteColor];
[self insertGif1];
}
-(void)drawCircleInRect
{
//draw colored circle here
CGContextRef context = [[NSGraphicsContext // 1
currentContext] graphicsPort];
// ********** Your drawing code here ********** // 2
CGContextSetFillColorWithColor(context,[self NSColorToCGColor:(_backgroundColor)]);
float radius1 = self.frame.size.height/2;
float startAngle = 0;
float endAngle = endAngle = PI*2;
CGPoint position = CGPointMake(self.frame.size.height/2,self.frame.size.height/2);//center of the view
CGContextBeginPath(context);
CGContextAddArc(context, position.x, position.y, radius1, startAngle, endAngle, 1);
CGContextDrawPath(context, kCGPathFill); // Or kCGPathFill
}
- (void)setBackgroundColor :(NSColor*)color
{
_backgroundColor = color;
[self setNeedsDisplay:YES];
}
- (CGColorRef)NSColorToCGColor:(NSColor *)color
{
NSInteger numberOfComponents = [color numberOfComponents];
CGFloat components[numberOfComponents];
CGColorSpaceRef colorSpace = [[color colorSpace] CGColorSpace];
[color getComponents:(CGFloat *)&components];
CGColorRef cgColor = CGColorCreate(colorSpace, components);
return cgColor;
}
//curentlly calling only this 1
- (void)insertGif1
{
[imageView removeFromSuperview];
[imageView setImageScaling:NSImageScaleNone];
[imageView setAnimates: YES];
imageView.image = [NSImage imageNamed:#"FanBlades11.gif"];
[self addSubview:imageView];
}
#end
Edit: I discovered the source of the problem:
I was adding my class (that represents gif inside the circle) on top of RMBlurredView
and the animations doesn't work when I adding it as subview ,However it works on all the other views I added.
Any ideas what could be the reason inside the RMBlurredView to stop my NSImageView from animating ?
Edit:
I think [self setWantsLayer:YES]; is the reason I am not getting animations
how can I still get the animation with this feature enabled?
Edit:
Here is a simple sample with my problem
http://snk.to/f-cdk3wmfn
my gif:This is my gif it is invisible on white background color
"You must disable the autoscaling feature of the NSImageView for the
animation playback to function. After you've done that, no extra
programming required. It works like a charm!"
--http://www.cocoabuilder.com/archive/cocoa/108530-nsimageview-and-animated-gifs.html
imageView.imageScaling = NSImageScaleNone;
imageView.animates = YES;
needed for layer backed views:
if the image view is in a layer backed view or is layer backed itself:
imageView.canDrawSubviewsIntoLayer = YES;
working example using the question's own gif:
NSImageView *view = [[NSImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
view.imageScaling = NSImageScaleNone;
view.animates = YES;
view.image = [NSImage imageNamed:#"FanBlades2_42x42.gif"];
view.canDrawSubviewsIntoLayer = YES;
NSView *layerview = [[NSView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
layerview.wantsLayer = YES;
[layerview addSubview:view];
[self.window.contentView addSubview:layerview];
I'm new to Objective-c and i've reached an issue.
I'm trying to make a small scoreboard app, for learning more of the language.
Here is a small part of the app.
What i want it it to do is to add a new UItext field when you press the add button. So on button click it looks like this.
How can i add multiple UItextfields on button click in objective-c?
This is a very simple requirement. All you have to do is to create and add the new "Player Number" text field and "Fouls" label each time you tap the "Add" button. After this, move the "Add" button downwards and that's it. Here is some example code.
First of all, to keep things simple I created a UIViewController subclass to hold the "Player Number" text box and the "Fouls" label to make it easy to create them multiple times. Here is the class:
// The .h file
#import <UIKit/UIKit.h>
#interface LLPlayerViewController : UIViewController
// This is a very simple class with no public properties or methods
#end
// The .m file
#implementation LLPlayerViewController
-(void)loadView {
// Create the initial view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 190, 25)];
self.view = view;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// PlayerNumber Text Field
UITextField *playerNumberField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 120, 20)];
playerNumberField.placeholder = #"PlayerNumber";
playerNumberField.borderStyle = UITextBorderStyleLine;
[self.view addSubview:playerNumberField];
// Fouls Label
UILabel *foulsLabel = [[UILabel alloc] initWithFrame:CGRectMake(135, 0, 50, 20)];
foulsLabel.text = #"0";
[self.view addSubview:foulsLabel];
}
#end
Then, in the main View Controller I create instances of that class and add them to the view each time I press the "Add" button. Here is some sample code:
// Don't forget to import the previous class
#import "LLPlayerViewController.h"
- (void)viewDidLoad
{
[super viewDidLoad];
// Inside viewDidLoad add the following code:
self.playersArray = [[NSMutableArray alloc] init];
// PLAYER Header
UILabel *playerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 25, 80, 20)];
playerLabel.text = #"PLAYER";
[self.view addSubview:playerLabel];
// FOULS Header
UILabel *foulsLabel = [[UILabel alloc] initWithFrame:CGRectMake(145, 25, 80, 20)];
foulsLabel.text = #"FOULS";
[self.view addSubview:foulsLabel];
// Add player button
self.addPlayerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.addPlayerButton.frame = CGRectMake(30, 60, 80, 30);
[self.addPlayerButton setTitle:#"Add" forState:UIControlStateNormal];
[self.addPlayerButton addTarget:self action:#selector(addPlayerTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.addPlayerButton];
}
// This method will get called each time the user taps the "Add" button
-(void)addPlayerTapped:(id)sender {
NSLog(#"Adding Player");
self.yOffset = 60.0;
LLPlayerViewController *llVC = [[LLPlayerViewController alloc] init];
llVC.view.alpha = 0.0;
[self.playersArray addObject:llVC];
[self.view addSubview:llVC.view];
[self repositionFields];
}
// This method is responsible for repositioning the views
-(void)repositionFields {
// Reposition each view in the array
for (LLViewController *llVC in self.playersArray) {
CGRect frame = llVC.view.frame;
frame.origin.x = 15;
frame.origin.y = self.yOffset;
llVC.view.frame = frame;
self.yOffset += frame.size.height;
}
// Add some animations to make it look nice
[UIView animateWithDuration:0.3 animations:^{
CGRect frame = self.addPlayerButton.frame;
frame.origin.y = self.yOffset;
self.addPlayerButton.frame = frame;
} completion:^(BOOL finished) {
LLViewController *llVC = [self.playersArray lastObject];
llVC.view.alpha = 1.0;
}];
}
This code was tested on an iPad simulator but I believe it would also work on an iPhone. The code is straight-forward and self explanatory but let me know if you have any more questions.
Hope this helps!
I'm just adding a quick intro screen with a button, which compiles and displays properly, but my button doesn't respond.
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"viewDidLoad!");
[self showIntro];
}
// make a quick Intro screen
- (void) showIntro {
UIImageView *myBackImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[myBackImage setImage:(UIImage *) [UIImage imageNamed:#"myBkgnd.png"]];
UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom ];
[clickBtn addTarget:self action:#selector(myButton_pressed:) forControlEvents:UIControlEventTouchUpInside];
[clickBtn setImage:(UIImage *) [UIImage imageNamed:#"btnImage.png"] forState:UIControlStateNormal];
clickBtn.frame = CGRectMake(111, 200, 75, 60);
[myBackImage addSubview:clickBtn];
[self.view addSubview:myBackImage];
}
- (void)myButton_pressed:(UIButton*)button {
NSLog(#"Button Pressed!");
}
What am I overlooking?
Thanks!
As you have added UIButton in UIImageView but didn't you forgot imageView's userInteraction by default NO. So :
myBackImage.userInteractionEnabled = YES;
EDIT : Just formatted
In my TestApp, I created a class "Carousel" which should let me create a swipe menu with a UIPageControl in an easy way (by simply creating an instance of the class Carousel).
Carousel is a subclass of UIView
At init, it creates an UIView, containing UIScrollView, UIPageControl
I can add further UIViews to the scroll view
I don't know if this is the proper way to do it, but my example worked quite well in my TestApp. Swiping between pages works perfectly and the display of the current page in the UIPageControl is correct.
If there were not one single problem: The UIPageControl sometimes reacts to clicks/taps (I only tested in Simulator so far!), sometimes it doesn't. Let's say most of the time it doesn't. I couldn't find out yet when it does, for me it's just random...
As you can see below, I added
[pageControl addTarget:self action:#selector(pageChange:) forControlEvents:UIControlEventValueChanged];
to my code. I thought this would do the proper handling of taps? But unfortunately, pageChange doesn't get always called (so the value of the UIPageControl doesn't change every time I click).
I would appreciate any input on this because I couldn't find any solution on this yet.
This is what I have so far:
Carousel.h
#import <UIKit/UIKit.h>
#interface Carousel : UIView {
UIScrollView *scrollView;
UIPageControl *pageControl;
BOOL pageControlBeingUsed;
}
- (void)addView:(UIView *)view;
- (void)setTotalPages:(NSUInteger)pages;
- (void)setCurrentPage:(NSUInteger)current;
- (void)createPageControlAt:(CGRect)cg;
- (void)createScrollViewAt:(CGRect)cg;
#end
Carousel.m
#import "Carousel.h"
#implementation Carousel
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Create a scroll view
scrollView = [[UIScrollView alloc] init];
[self addSubview:scrollView];
scrollView.delegate = (id) self;
// Init Page Control
pageControl = [[UIPageControl alloc] init];
[pageControl addTarget:self action:#selector(pageChange:) forControlEvents:UIControlEventValueChanged];
[self addSubview:pageControl];
}
return self;
}
- (IBAction)pageChange:(id)sender {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * pageControl.currentPage;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:TRUE];
NSLog(#"%i", pageControl.currentPage);
}
- (void)addView:(UIView *)view {
[scrollView addSubview:view];
}
- (void)createPageControlAt:(CGRect)cg {
pageControl.frame = cg;
}
- (void)setTotalPages:(NSUInteger)pages {
pageControl.numberOfPages = pages;
}
- (void)setCurrentPage:(NSUInteger)current {
pageControl.currentPage = current;
}
- (void)createScrollViewAt:(CGRect)cg {
[scrollView setPagingEnabled:TRUE];
scrollView.frame = cg;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*pageControl.numberOfPages, scrollView.frame.size.height);
[scrollView setShowsHorizontalScrollIndicator:FALSE];
[scrollView setShowsVerticalScrollIndicator:FALSE];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
float frac = scrollView.contentOffset.x / scrollView.frame.size.width;
NSUInteger page = lround(frac);
pageControl.currentPage = page;
}
#end
ViewController.m (somewhere in viewDidLoad)
Carousel *carousel = [[Carousel alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
for (int i=0; i<5; i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * 320, 0, 320, 420)];
UIColor *color;
if(i%3==0) color = [UIColor blueColor];
else if(i%3==1) color = [UIColor redColor];
else color = [UIColor purpleColor];
view.backgroundColor = color;
[carousel addView:view];
view = nil;
}
[carousel setTotalPages:5];
[carousel setCurrentPage:0];
[carousel createPageControlAt:CGRectMake(0,420,320,40)];
[carousel createScrollViewAt:CGRectMake(0,0,320,420)];
Your code is correct. Most likely the frame of your pageControl is pretty small, so theres not a lot of area to look for touch events. You would need to increase the size of the height of pageControl in order to make sure taps are recognized all of the time.