Another UIButton Background Color Problem

Another simple problem with changing the background color of a UIButton. Therefore, I decided that I had a little thought in creating an image or for viewing.

I created a button in the storyboard and plugged it into a power outlet. In my VC class, I have the following

.h

@property (nonatomic, readwrite, strong) IBOutlet UIButton *uiLoginButton;

.m

- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%s and frame rect %@", __PRETTY_FUNCTION__, self.uiLoginButton); //here uiloginbutton does not have any size!

CGRect buttonRect = CGRectMake(50, 50, 100.0, 100.0);

self.uiLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.uiLoginButton.frame = buttonRect;
self.uiLoginButton.backgroundColor = [UIColor redColor];
NSLog(@"%s and frame rect %@", __PRETTY_FUNCTION__, self.uiLoginButton);    
}

No. I set the values ​​in CGRectMake randomly since the frame of the IBOutlet button is 0. How did it happen?

What is the correct way to change the color of a button?

+5
source share
3 answers

If you create a button in nib, you can change all properties using nib itself. Like this enter image description here

OR

If you want to do it again since you are adding it via nib points, check

  • No need to initialize. Since this property is from IB.
  • IB -setFrame:
  • , -setBackgroundcolor:

, .h

@property (nonatomic,strong) IBOutlet UIButton *uiLoginButton;

.m

[self.uiLoginButton setFrame:buttonRect];
[self.uiLoginButton setBackgroundColor:[UIColor redColor]];
+14

, : https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html

UIButton storyBoard, NSLog viewDidAppear.

RounRect , storyBoard, .

, , :

[ setBackgroundImage: (UIImage *) forState: UIControlStateNormal];

+1

you override the value self.uiLoginButton

delete these lines

CGRect buttonRect = CGRectMake(50, 50, 100.0, 100.0);

self.uiLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.uiLoginButton.frame = buttonRect;
0
source

All Articles