I have subclassed UIControl to create my own class. I am trying to override setHighligheted: but for some reason it doesn't get called...
I have double checked and none of my subviews have userInteractionEnabled = NO. Are there any other methods that I should override?.
Thank you!
Here is some sample code:
#import "Booking.h"
#interface BookingCloud : UIControl
// Booking
#property(nonatomic, strong) Booking *booking;
// BackgroundView
#property (strong, nonatomic) IBOutlet UIView *backgroundView;
And the implementation
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setUpSubViews];
}
return self;
}
- (void)setUpSubViews {
[[NSBundle mainBundle] loadNibNamed:#"BookingCloud" owner:self options:nil];
[self addSubview:backgroundView];
self.frame = backgroundView.frame;
self.layer.cornerRadius = 10;
self.layer.masksToBounds = YES;
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
backgroundView.backgroundColor = [[Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:#"background_color"]] colorWithAlphaComponent:0.8];
} else {
backgroundView.backgroundColor = [[Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:#"background_color"]] colorWithAlphaComponent:1];
}
}
Related
I have a custom view about alertView.
But when I init my alertView, it seem not set my awakeFromNib value of label.
What's wrong about my code?
I can't figure out this issue.
Thanks.
AlertView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
#interface AlertView : UIView
#property (weak, nonatomic) IBOutlet UILabel *labTitle;
#property (weak, nonatomic) IBOutlet UILabel *labMessage;
#property (weak, nonatomic) IBOutlet UIButton *btnCancel;
#property (weak, nonatomic) IBOutlet UIButton *btnConfirm;
#end
AlertView.m
#import "AlertView.h"
#interface AlertView ()
#property (weak, nonatomic) IBOutlet UIView *headerView;
#end
#implementation AlertView
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(!self){
return nil;
}
self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
owner:self
options:nil] firstObject];
self.frame = frame;
return self;
}
- (void)awakeFromNib{
[super awakeFromNib];
// custom label text not show.
self.labTitle.text = #"123";
self.labMessage.text = #"456";
[self.btnCancel setTitle:#"789"; forState:UIControlStateNormal];
[self.btnConfirm setTitle:#"111"; forState:UIControlStateNormal];
self.btnCancel.layer.cornerRadius = 5.f;
self.btnConfirm.layer.cornerRadius = 5.f;
self.headerView.layer.cornerRadius = 10;
}
#end
Viewcontroller.m
#interface Viewcontroller ()
#property AlertView * alertView;
#end
#implementation Viewcontroller
- (void)viewDidLoad {
[self popupView];
}
- (void)popupView{
CGRect viewSize = CGRectMake(16, 100, [[UIScreen mainScreen] bounds].size.width - 16 * 2, self.height);
self.alertView = [[ResultAlertView alloc] initWithFrame:viewSize];
self.alertView.layer.cornerRadius = 10;
[self SetShadowForView:self.alertView];
[self.alertView.btnConfirm addTarget:self action:#selector(cancelStartUsingView:) forControlEvents:UIControlEventTouchUpInside];
[self.alertView.btnCancel addTarget:self action:#selector(cancelStartUsingView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.alertView];
}
- (IBAction)cancelStartUsingView:(UIButton *)sender{
//Btn also not work.
NSLog(#"123");
}
#end
you can use:
- (void)layoutSubviews {
[super layoutSubviews];
self.labTitle.text = #"123";
self.labMessage.text = #"456";
[self.btnCancel setTitle:#"789" forState:UIControlStateNormal];
[self.btnConfirm setTitle:#"111"forState:UIControlStateNormal];
self.btnCancel.layer.cornerRadius = 5.f;
self.btnConfirm.layer.cornerRadius = 5.f;
self.headerView.layer.cornerRadius = 10;
}
and call
- (void)viewDidLoad {
dispatch_async(dispatch_get_main_queue(), ^{
[self popupView];
});
}
I have made a UITableViewCell with xib, inside it I have made a UIView and made IBoutlet in my custom tableViewCell class. I want to set the border color of that UIView.
My code in tableViewCell.h:
#property (weak, nonatomic) IBOutlet UIView *circleView;
In tableViewCell.m:
#import "OUSTProfileTableViewCell.h"
#implementation OUSTProfileTableViewCell
//#synthesize circleView = _circleView;
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
self.circleView.layer.cornerRadius = 3; // this value vary as per your desire
self.circleView.layer.masksToBounds = YES;
self.circleView.layer.borderWidth = 2.0;
self.circleView.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor lightGrayColor]);
}
return self;
}
#end
But it's not working.
Put code inside
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
self.circleView.layer.cornerRadius = 3; // this value vary as per your desire
self.circleView.layer.masksToBounds = YES;
self.circleView.layer.borderWidth = 2.0;
self.circleView.layer.borderColor = [UIColor lightGrayColor].CGColor;
}
I create a class named SWTextView and it inherits from UITextView. It does nothing but add a placeholder label. When I initialize a SWTextView and put it in a UITableViewCell, it's not editable even when the editable property of the SWTextView is YES. Does anyone have met the same problem?
The SWTextView is as follow:
#import <UIKit/UIKit.h>
#interface SWTextView : UITextView
- (void)setPlaceholder:(NSString *)placeholder;
#end
#interface SWTextView ()
#property(nonatomic, strong) UILabel *placeholderLabel;
#end
#implementation SWTextView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self = [super initWithFrame:frame]) {
_placeholderLabel = [[UILabel alloc] init];
_placeholderLabel.textColor = [UIColor grayColor];
[self addSubview:_placeholderLabel];
self.font = [UIFont systemFontOfSize:12];
}
return self;
}
- (void)setFont:(UIFont *)font
{
[super setFont:font];
_placeholderLabel.font = font;
}
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholderLabel.text = placeholder;
}
- (void)layoutSubviews
{
[super layoutSubviews];
[_placeholderLabel sizeToFit];
}
#end
I'm using a custom UIView in myViewController. I have a SampleView which is my custom view. I have my class as..
SampleView.h
#property (strong, nonatomic) IBOutlet UIView *mainView;
#property (strong, nonatomic) IBOutlet UIButton *sampleButton;
and in the SampleView.m
I have added a basic init
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
UIView *view = nil;
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"SampleView"
owner:self
options:nil];
for (id object in objects) {
if ([object isKindOfClass:[UIView class]]) {
view = object;
break;
}
}
if (view != nil) {
_mainView = view;
view.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:view];
[self setNeedsUpdateConstraints];
}
}
Now in my ViewController I want to change the property of the title on the sampleButton...
SampleView *sampleView = [[SampleView alloc]init];
sampleView.sampleButton.titleLabel.text = #"Hello";
[self.view addSubview:sampleView];
There is no change in the SubView's button text. Please do help me about the same.
Thanks.
I think
- (void)commonInit
{
//UIView *view = nil;
UIView *view=[[UIView all]init];
........
}
I am trying to subclass a UIView using the nib. Using the following code:
- (void)awakeFromNib
{
[super awakeFromNib];
NSArray *v = [[NSBundle mainBundle] loadNibNamed:#"Qus_Scale1to7View" owner:self options:nil];
[self addSubview:[v objectAtIndex:0]];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
NSArray *v = [[NSBundle mainBundle] loadNibNamed:#"Qus_Scale1to7View" owner:self options:nil];
[self addSubview:[v objectAtIndex:0]];
}
return self;
}
this creates the object correctly and view also displayed and when the object loads from its nib the delegate instantly becomes null and ignores any attempt to assign values to it.
Can anyone know the reason why it is?
Thanks in advance.
It won't work reusing same xib for multiple view controllers. If you want to reuse that view make a class that inherits from UIView and add the code there.
#import "SomeProtocol.h"
#interface MyCustomView : UIView {
IBOutlet UIView *headerView;
IBOutlet UIView *footerView;
IBOutlet UIButton *updateBtn;
}
#property (nonatomic, assign) id<SomeProtocol> delegate;
#end
............
#implementation BCFirmwareView
#synthesize delegate = _delegate;
+ (id)viewFromNibWithName: (NSString*)name {
UIView *view = nil;
NSArray *views = [[NSBundle mainBundle] loadNibNamed: name owner: self options: nil];
if (views) {
for (UIView *aView in views) {
if ([aView isKindOfClass: NSClassFromString(name)])
view = aView;
}
}
return view;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder: aDecoder];
if (self) {
}
return self;
}
- (id)init {
self = [[MyCustomView viewFromNibWithName: #"MyCustomView"] retain];
if (self) {
}
return self;
}
- (void)dealloc {
self.delegate = nil;
[headerView release];
[footerView release];
[updateBtn release];
[super dealloc];
}
- (void)awakeFromNib {
[super awakeFromNib];
// Do any additional setup after loading the view from its nib.
headerView.backgroundColor = [UIColor redColor];
footerView.backgroundColor = [UIColor greenColor];
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview: newSuperview];
if (!newSuperview)
return;
}
- (void)didMoveToSuperview {
[super didMoveToSuperview];
}
- (IBAction)updateBtnPressed: (id)sender {
// do some stuff
}
#end
The next step is to open the xib in Interface Builder and set your class as the custom class for the view, not for the File's Responder. Right click on the view and make the outlet and actions connections.
Now you should be able to simply make instances of your MyCustomView in any view controller and use it. Will work from Interface Builder as well if you don't forget to change your view custom class to your class.
You can create a custom UIView with Xib and add properties to it.
Then you link the class with the xib and link the properties with the IB.
Or you can only use the
NSArray *v = [[NSBundle mainBundle] loadNibNamed:#"Qus_Scale1to7View" owner:self options:nil];
UIView *view = [v objectAtIndex:0];
and set your objects values using viewWithTag: method.
UILabel *label = (UILabel *)[view viewWithTag:yourTag];
Let me know if this helps.