Cannot add dznsegmentedcontrol to UIView - ios

I am trying to add the following control to a UIView that is inside a viewcontroller. The control does not seem to appear inside the view.
https://www.cocoacontrols.com/controls/dznsegmentedcontrol
NSArray *items = #[#"Kicks", #"Re-kicks", #"Interested"];
DZNSegmentedControl *control = [[DZNSegmentedControl alloc] initWithItems:items];
control.tintColor = [UIColor blueColor];
control.delegate = self;
control.selectedSegmentIndex = 2;
[control addTarget:self action:#selector(selectedSegment:) forControlEvents:UIControlEventValueChanged];
[cell.segmentView addSubview:control];

I think what you are looking for is to add the controller somehow.
[self.navigationController.navigationBar addSubview:self.control];
or as in the example code givin by dzenbot:
self.tableView.tableHeaderView = self.control;

Related

How to initialize and add a subview to a custom UIView?

I have a custom view inherited from UIView. Now I want to add a UISegmentedControl as a subview to it.
So the first question is: should the property of UISegmentedControl be weak or strong? (With IBOutlets I know that Apple recommends using strong since 2015).
And the second question is where do I initialize it and set its layout. As I understand I shouldn't do this in the drawRect: method. Should it be initialized in initWithFrame: method, added as a subview to my custom view and then its layout to be set in layoutSubviews like so:
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSArray *options = #[#"option1", #"option2", #"option3"];
_segmentedControl = [[UISegmentedControl alloc] initWithItems:options];
[_segmentedControl addTarget:self action:#selector(someAction:) forControlEvents:UIControlEventValueChanged];
[self addSubview:_segmentedControl];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect segmentedControlFrame = CGRectMake(self.bounds.size.width / 4.0, 50, self.bounds.size.width / 2.0, 30);
self.segmentedControl.frame = segmentedControlFrame;
self.segmentedControl.tintColor = [UIColor blackColor];
[self.segmentedControl setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateSelected];
}
or just do all this in layoutSubviews: method:
- (void)layoutSubviews {
NSArray *options = #[#"option1", #"option2", #"option3"];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:options];
CGRect segmentedControlFrame = CGRectMake(self.bounds.size.width / 4.0, 50, self.bounds.size.width / 2.0, 30);
segmentedControl.frame = segmentedControlFrame;
segmentedControl.tintColor = [UIColor blackColor];
[segmentedControl setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateSelected];
[segmentedControl addTarget:self action:#selector(someAction:) forControlEvents:UIControlEventValueChanged];
[self addSubview:segmentedControl];
}
You should use strong if your view will keep the segmented control if it is not contained in it's hierarchy. You can use weak when the segmented control is always part of the hierarchy, but you have to keep it with a strong reference until you add it to the hierarchy. In your first code snippet you must use a local (strong) variable, which holds the control:
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSArray *options = #[#"option1", #"option2", #"option3"];
UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:options];
[control addTarget:self action:#selector(someAction:) forControlEvents:UIControlEventValueChanged];
[self addSubview:control]; // By now the view keeps the control
self.segmentedControl = control;
}
return self;
}
The advantage of a weak reference is that it is automatically resetted, if you remove the control from the hierarchy. This may help you to keep the amount memory small.
For the layout you should prefer autolayout contraints. Autosizing is possible, too (take a look at translatesAutoresizingMaskInotConstraints anyway). layoutSubviews shouldn't add the segment to the view, because it is called several times.

AddSubView is not working in UIScrollView

I need to place my labels and other objects into a scrollable view. But it doesn't add my 2 labels, I only get a white background everytime I run my program
- (void) initLayout:(CGRect)frame
{
self.backgroundColor = [UIColor whiteColor];
UIFont *customFont = [UIFont fontWithName:#"HelveticaNeue" size:40.0]; //custom font
scrollView = [[UIScrollView alloc] initWithFrame:frame];
[self addSubview:scrollView];
oponent = [[UILabel alloc] init];
oponent.font = customFont;
oponent.textColor = [UIColor blackColor];
oponent.textAlignment = NSTextAlignmentCenter;
oponent.text = #"TEST";
[scrollView addSubview:oponent];
proponent = [[UILabel alloc] init];
proponent.font = customFont;
proponent.textColor = [UIColor blackColor];
proponent.textAlignment = NSTextAlignmentCenter;
proponent.text = #"TRY";
[scrollView addSubview:proponent];
}
You are not setting frames for proponent and opponent labels. Set their frames and also set content size for scrollview. You can also check by setting some border colors to your labels.
To check if your scrollview is working add UIScrollViewDelegate in your header file and then use the delegate methods.
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
Add the following delegate method and use breakpoint to check if it is called
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
}
You Haven't set frame to label
oponent = [[UILabel alloc] init];
proponent = [[UILabel alloc] init];
set frames
CGRect labelframe // Customize according to your need
oponent = [[UILabel alloc] initWithFrame:labelframe]; // missing
proponent = [[UILabel alloc] initWithFrame:labelframe]; // missing

pull to refresh not working in collection view when collection view have no data in ios

This is my refresh control in worldview method.
self.colView.refreshControl = [[UIRefreshControl alloc] init];
self.colView.refreshControl.backgroundColor = [UIColor purpleColor];
self.colView.refreshControl.tintColor = [UIColor whiteColor];
[self.colView.refreshControl addTarget:self
action:#selector(refresh)
forControlEvents:UIControlEventValueChanged];
Please try this code. Collection view lost our height.
self.colView.alwaysBounceVertical = YES;
Your code looks fine, Just you have to move tableview at top and set some frame before adding it to collectionview. This will work even if your collection view content view have 0 frame size:
-(void)addPullToRefressToTableView {
UIView *refreshView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 0, 0)];
[self.tableView insertSubview:refreshView atIndex:0];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.tintColor = [UIColor grayColor];
[refreshControl addTarget:self action:#selector(reloadDatas) forControlEvents:UIControlEventValueChanged];
NSMutableAttributedString *refreshString = [[NSMutableAttributedString alloc] initWithString:#"Pull To Refresh"];
[refreshView addSubview:refreshControl];
}

UISegmentedControl setFrame not working

I am trying to add segmented control to tableviewheader. I am using the following code to do that
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"ALL", #"HOUSE", #"SENATE", nil]];
segmentedControl.frame = CGRectMake(24, 0, 272, 30);
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor grayColor];
[self.view addSubview:segmentedControl];
segmentedControl.selectedSegmentIndex = 0;
self.tableView.tableHeaderView = segmentedControl;
I can't set the frame, no matter what value is put its always 100% wide. How can i set the add 24px margin on left and right ?
That is because you are using the segment control as a tableHeaderView. a UITableViewHeaderView will be always be as wide as your tableView. You can only change the height.
Adding to akshaynhegde answer, you can easily achieve this by adding an extra UIView as parent and add the UISegmentControl to that UIView. In this case the parent UIView will take the whole width of UITableView but not the UISegmentControl.
So following should be the change in your code
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0,0,320.0,30.0)];
[view addSubview:segmentedControl];
self.tableView.tableHeaderView = view;
It should solve your problem.
Cheers.
First, you are adding segmentedControl on controller view [self.view addSubview:segmentedControl] and then assigning it to table header view
self.tableView.tableHeaderView = segmentedControl;
Which is wrong, you need to create a view with size table header size and then add segmentedControl add on view and assign this view to table header view.
Example
**UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"ALL", #"HOUSE", #"SENATE", nil]];
segmentedControl.frame = CGRectMake(24, 0, 272, 30);
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor grayColor];
[view addSubview:segmentedControl];
self.tableView.tableHeaderView = view;**

Store Views in an array

I have three buttons and one view controller. I want to link three buttons to those three
different views (pages).
I've tried this by making three views and storing it in an mutable array.
ViewArray= [[NSMutableArray alloc]initWithCapacity:15];
CGRect viewRect = CGRectMake(10, 10, 100, 10);
//VIEW1
UIView* view1 = [[UIView alloc] initWithFrame:viewRect];
view1.backgroundColor = [UIColor redColor];
//VIEW2
UIView* view2 = [[UIView alloc] initWithFrame:viewRect];
view2.backgroundColor = [UIColor greenColor];
I added three buttons programmatically. After that for the action of each button I used this:
//action for each button
self.view = [ViewArray objectAtIndex:i];//where i={1,2,3} for different functions
but I am not getting what I want.
A better design would be to add all of the views invisibly, then change their opacity 0.0-1.1 to hide and show. (also, a key mistake in the OP is that the code replaces the view controller view with the subviews, instead of adding them)
CGRect viewRect = CGRectMake(10, 10, 100, 10);
//VIEW1
UIView* view1 = [[UIView alloc] initWithFrame:viewRect];
view1.backgroundColor = [UIColor redColor];
view1.tag = 1;
view1.alpha = 0.0;
[self.view addSubview:view1];
//VIEW2
UIView* view2 = [[UIView alloc] initWithFrame:viewRect];
view2.backgroundColor = [UIColor greenColor];
view1.tag = 2;
view1.alpha = 0.0;
[self.view addSubview:view2];
Add a property in your vc's interface that keeps track of the visible view:
#property(nonatomic, weak) UIView *visibleSubview;
Then the button action:
UIView *subview = [self.view viewWithTag:i];
//where i={1,2,3} for different functions
// i really needs to be 1,2,3 here, to correspond to the tags
[UIView animateWithDuration:0.5 animations:^{
subview.alpha = 1.0;
self.visibleSubview.alpha = 0.0;
} completion:^(BOOL finished) {
self.visibleSubview = subview;
}];
Well I'm not sure exactly what you want. But in general if you want to access any change multiple objects such as a UIView or UILabels, etc... You can just use and NSArray and a for loop to access and make all the changes without any extra code copies like so:
First of all, setup the NSArray with all your objects like so:
NSArray *_viewArray = #[view1, view2]; //etc....
And also setup an array with the different UIColors like so:
NSArray *_colourArray = #[[UIColor redColor], [UIColor greenColor]]; //etc....
And then when you want to access them and edit multiple UIViews or what ever objects, you can just use a for loop like so:
for (int loop = 0; loop < ([_viewArray count] - 1); loop++) {
((UIView*)_viewArray[loop]).backgroundColor = ((UIColor*)_colourArray[loop]);
}
I hope this helps you :)

Resources