Not able to add subview to my UIView - ios

I have a UIView element on the screen. It is connected by an IBOutlet radioButtonGroupView. I am trying to add a subview to that element and it is not working. The subview is never added.
Here is my code:
-(id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
subView.backgroundColor = [UIColor yellowColor];
[self.radioButtonGroupView addSubview:subView];
return self;
}

You will probably be better off if you make this element a subclass of a UIViewController (instead of a UIView).
Then put the subview loading code inside viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad]; //not really needed but it doesn't hurt
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
subView.backgroundColor = [UIColor yellowColor];
[self.radioButtonGroupView addSubview:subView];
}
EDIT:
You say that you don't get any frame information on self.radioButtonGroupView in your viewDidLoad. That is almost a certain sign that your IBOutlet is not properly connected to the element in InterfaceBuilder. You can always make a simple test:
- (void)viewDidLoad
{
[super viewDidLoad]; //not really needed but it doesn't hurt
NSLog (#"self.radioButtonGroupView: %#",self.radioButtonGroupView);
[self.radioButtonGroupView setHidden:YES];
}
If it is still shown - then it simply isn't properly connected. IB has it's ways. Simple
deletion of IBOutlet connection and reconnecting it again might do the trick. Also: it your radioButtonGroupView is not UIView but a subclass of UIView, make sure that it's header file is imported in your MyViewController.m file and class properly defined in .xib.

Related

Copying UIButtons frame gives wrong position

I want to create another UIView / UIButton on an UIButton so I'm getting its' frame and use it in declaration of another UIView but It creates like below screenshot (with blue view). By the way, button is created on storyboard.
I'm using Xcode 9.1 and iOS 11.1
- (void)viewDidLoad {
UIView *picker = [[UIView alloc] initWithFrame:_mButton.frame];
picker.backgroundColor=[UIColor blueColor];
[self.view addSubview: picker];
}
Screenshot 1
Screnshot 2
I guess the proper frames are not calculated in viewDidLoad. You can try to make picker a property and adjust its frame in viewDidLayoutSubviews
#property (strong, nonatomic) UIView *picker;
- (void)viewDidLoad {
[super viewDidLoad];
self.picker = [[UIView alloc] initWithFrame:_mButton.frame];
self.picker.backgroundColor=[UIColor blueColor];
[self.view addSubview: self.picker];
}
- (void)viewDidLayoutSubviews {
self.picker.frame = _mButton.frame;
[super viewDidLayoutSubviews];
}
But off course using storyboard and autolayout would probably be the best sollution if you have this possibility.

UIScrollView as subview of UIView does not work with autolayout

I created a customView (UIView) which contains a UIScrollView as its subview, and
objects I put on the scrollView did not show. To test it, I added a UIView to the scrollView and it didn't show neither(see code example below). Since the content I want to put on the scrollView is dynamic, I want to approach the problem by just using AutoLayout. I tried different ways and followed instructions online, none of it worked, all the similar questions were UIScrollView created inside of a UIViewController.
Here is my code, I'm using Masonry:
#interface CustomView : UIView
#end
#implementation CustomView
- (instancetype)init {
if (self = [super init]) {
self.scrollView = [[UIScrollView alloc]init];
self.scrollView.backgroundColor = [UIColor redColor];
[self addSubview:self.scrollView];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
//Here I add someView to the scrollView, and when I create an instance of the scrollView, the someView does not show.
self.someView = [[UIView alloc]init];
self.someView.backgroundColor = [UIColor blackColor];
[self.scrollView addSubview:self.someView];
[self.someView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.scrollView);
make.size.equalTo(CGSizeMake(40, 40));
}];
}
return self;
}
Here is how I create an instance of the CustomView
CustomView *customView = [[CustomView alloc]init];
[self.view addSubview: customView];
[customView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(64.0);
make.height.equalTo(40.0);
make.leading.trailing.equalTo(0);
}];
Is there anything I missed or did wrong that caused someView to not show?
Thank your for your help.
I solved the problem by: 1).Using scrollView directly instead of as a subView in a UIView (my customView). 2) make sure to add the right and bottom constraints of the scrollView.

Add a subclass of subview to a subclass of UIView

I'm trying to do something very basic. I want to add a subView to my UIView subclass. I assume that I would put this in initWithFrame method as below, but view that are instances of this class do not draw this subview. What am I doing wrong?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
redView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
[redView setBackgroundColor:[UIColor redColor]];
[self addSubview:redView];
}
return self;
}
BTW redView is a property defined in the header of the sub class, like:
#property (strong, nonatomic) UIView *redView;
Thanks for reading!
You should place your initializing code inside:
- (id)initWithCoder:(NSCoder *)aDecoder { ... }
or
- (void)awakeFromNib { ... }
These methods are called when a view is loaded from nib. Don't forget to call [super ...] in the above methods.

Subclassing UIView containing multiple UIViews

I want to create a UIView subclass which will in its initializer add a UIView to its own view, like:
[self addSubview: someKindOfUIView];
This is how I've implemented it in the implementation file:
- (id)init
{
self = [super initWithFrame:CGRectMake(0, 0, 110, 110)];
if (self) {
self.grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
self.grayView.backgroundColor = [UIColor grayColor];
[self addSubview:self.grayView];
[self setBackgroundColor:[UIColor blueColor]];
}
return self;
}
But when I try to use instances of this class, the instances only show a bluebox, not a blue box containing a gray box. How can i fix that, is it possible? :)
Okay, after some testing and research I found the answer!
In my .h file I had a weak pointer to the grayView property:
#property (nonatomic,weak) UIView *grayView;
Instead, it should be:
#property (nonatomic,strong) UIView *grayView;
I sorta understand why, but i can't explain it in a good way, so if anyone can explain why (in an easy way) grayView has to have a strong pointer instead of a weak one, please comment under this answer ;)

How to add multiple UIViews in a UIScrollView

I have a UIView that draws a pie chart and I would like to put 3 or maybe 4 charts in a UIScrollView programatically. How can I do that?
my h file is like this
#class PieChart;
#interface ViewController : UIViewController {
}
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
and m file
- (void)viewDidLoad
{
[super viewDidLoad];
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
PieChart *chart = [[PieChart alloc]initWithFrame:CGRectZero];
[chart setFrame:CGRectMake(100, 100, 200, 200)];
[scrollView addSubview:chart];
}
If you need to add subview to the view you need to use [view addSubview:subview]. This is common practice.
Read about frames and bounds (coordinate systems for view and sub views):
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaViewsGuide/Coordinates/Coordinates.html
As I understand you have a problem due to content size of scroll view. Read more here:
http://developer.apple.com/library/ios/#DOCUMENTATION/WindowsViews/Conceptual/UIScrollView_pg/Introduction/Introduction.html
Hope it helps.
You can just add it with:
[scrollView addSubview:singleView];
You add multiple subviews to a UIScrollView just like you add a single subview; with addSubview:. What problems are you running into?

Resources